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
:
924 unreachable("switch statement above should be complete");
928 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
929 bool array
, glsl_base_type type
)
932 case GLSL_TYPE_FLOAT
:
934 case GLSL_SAMPLER_DIM_1D
:
935 return (array
? image1DArray_type
: image1D_type
);
936 case GLSL_SAMPLER_DIM_2D
:
937 return (array
? image2DArray_type
: image2D_type
);
938 case GLSL_SAMPLER_DIM_3D
:
940 case GLSL_SAMPLER_DIM_CUBE
:
941 return (array
? imageCubeArray_type
: imageCube_type
);
942 case GLSL_SAMPLER_DIM_RECT
:
946 return image2DRect_type
;
947 case GLSL_SAMPLER_DIM_BUF
:
951 return imageBuffer_type
;
952 case GLSL_SAMPLER_DIM_MS
:
953 return (array
? image2DMSArray_type
: image2DMS_type
);
954 case GLSL_SAMPLER_DIM_SUBPASS
:
955 return subpassInput_type
;
956 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
957 return subpassInputMS_type
;
958 case GLSL_SAMPLER_DIM_EXTERNAL
:
963 case GLSL_SAMPLER_DIM_1D
:
964 return (array
? iimage1DArray_type
: iimage1D_type
);
965 case GLSL_SAMPLER_DIM_2D
:
966 return (array
? iimage2DArray_type
: iimage2D_type
);
967 case GLSL_SAMPLER_DIM_3D
:
970 return iimage3D_type
;
971 case GLSL_SAMPLER_DIM_CUBE
:
972 return (array
? iimageCubeArray_type
: iimageCube_type
);
973 case GLSL_SAMPLER_DIM_RECT
:
976 return iimage2DRect_type
;
977 case GLSL_SAMPLER_DIM_BUF
:
980 return iimageBuffer_type
;
981 case GLSL_SAMPLER_DIM_MS
:
982 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
983 case GLSL_SAMPLER_DIM_SUBPASS
:
984 return isubpassInput_type
;
985 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
986 return isubpassInputMS_type
;
987 case GLSL_SAMPLER_DIM_EXTERNAL
:
992 case GLSL_SAMPLER_DIM_1D
:
993 return (array
? uimage1DArray_type
: uimage1D_type
);
994 case GLSL_SAMPLER_DIM_2D
:
995 return (array
? uimage2DArray_type
: uimage2D_type
);
996 case GLSL_SAMPLER_DIM_3D
:
999 return uimage3D_type
;
1000 case GLSL_SAMPLER_DIM_CUBE
:
1001 return (array
? uimageCubeArray_type
: uimageCube_type
);
1002 case GLSL_SAMPLER_DIM_RECT
:
1005 return uimage2DRect_type
;
1006 case GLSL_SAMPLER_DIM_BUF
:
1009 return uimageBuffer_type
;
1010 case GLSL_SAMPLER_DIM_MS
:
1011 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
1012 case GLSL_SAMPLER_DIM_SUBPASS
:
1013 return usubpassInput_type
;
1014 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
1015 return usubpassInputMS_type
;
1016 case GLSL_SAMPLER_DIM_EXTERNAL
:
1023 unreachable("switch statement above should be complete");
1027 glsl_type::get_array_instance(const glsl_type
*base
,
1028 unsigned array_size
,
1029 unsigned explicit_stride
)
1031 /* Generate a name using the base type pointer in the key. This is
1032 * done because the name of the base type may not be unique across
1033 * shaders. For example, two shaders may have different record types
1037 snprintf(key
, sizeof(key
), "%p[%u]x%uB", (void *) base
, array_size
,
1040 mtx_lock(&glsl_type::hash_mutex
);
1041 assert(glsl_type_users
> 0);
1043 if (array_types
== NULL
) {
1044 array_types
= _mesa_hash_table_create(NULL
, _mesa_hash_string
,
1045 _mesa_key_string_equal
);
1048 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
1049 if (entry
== NULL
) {
1050 const glsl_type
*t
= new glsl_type(base
, array_size
, explicit_stride
);
1052 entry
= _mesa_hash_table_insert(array_types
,
1057 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
1058 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
1059 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
1061 glsl_type
*t
= (glsl_type
*) entry
->data
;
1063 mtx_unlock(&glsl_type::hash_mutex
);
1069 glsl_type::compare_no_precision(const glsl_type
*b
) const
1074 if (this->is_array()) {
1075 if (!b
->is_array() || this->length
!= b
->length
)
1078 const glsl_type
*b_no_array
= b
->fields
.array
;
1080 return this->fields
.array
->compare_no_precision(b_no_array
);
1083 if (this->is_struct()) {
1084 if (!b
->is_struct())
1086 } else if (this->is_interface()) {
1087 if (!b
->is_interface())
1093 return record_compare(b
,
1094 true, /* match_name */
1095 true, /* match_locations */
1096 false /* match_precision */);
1100 glsl_type::record_compare(const glsl_type
*b
, bool match_name
,
1101 bool match_locations
, bool match_precision
) const
1103 if (this->length
!= b
->length
)
1106 if (this->interface_packing
!= b
->interface_packing
)
1109 if (this->interface_row_major
!= b
->interface_row_major
)
1112 /* From the GLSL 4.20 specification (Sec 4.2):
1114 * "Structures must have the same name, sequence of type names, and
1115 * type definitions, and field names to be considered the same type."
1117 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
1119 * Section 7.4.1 (Shader Interface Matching) of the OpenGL 4.30 spec says:
1121 * "Variables or block members declared as structures are considered
1122 * to match in type if and only if structure members match in name,
1123 * type, qualification, and declaration order."
1126 if (strcmp(this->name
, b
->name
) != 0)
1129 for (unsigned i
= 0; i
< this->length
; i
++) {
1130 if (match_precision
) {
1131 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
1134 const glsl_type
*ta
= this->fields
.structure
[i
].type
;
1135 const glsl_type
*tb
= b
->fields
.structure
[i
].type
;
1136 if (!ta
->compare_no_precision(tb
))
1139 if (strcmp(this->fields
.structure
[i
].name
,
1140 b
->fields
.structure
[i
].name
) != 0)
1142 if (this->fields
.structure
[i
].matrix_layout
1143 != b
->fields
.structure
[i
].matrix_layout
)
1145 if (match_locations
&& this->fields
.structure
[i
].location
1146 != b
->fields
.structure
[i
].location
)
1148 if (this->fields
.structure
[i
].offset
1149 != b
->fields
.structure
[i
].offset
)
1151 if (this->fields
.structure
[i
].interpolation
1152 != b
->fields
.structure
[i
].interpolation
)
1154 if (this->fields
.structure
[i
].centroid
1155 != b
->fields
.structure
[i
].centroid
)
1157 if (this->fields
.structure
[i
].sample
1158 != b
->fields
.structure
[i
].sample
)
1160 if (this->fields
.structure
[i
].patch
1161 != b
->fields
.structure
[i
].patch
)
1163 if (this->fields
.structure
[i
].memory_read_only
1164 != b
->fields
.structure
[i
].memory_read_only
)
1166 if (this->fields
.structure
[i
].memory_write_only
1167 != b
->fields
.structure
[i
].memory_write_only
)
1169 if (this->fields
.structure
[i
].memory_coherent
1170 != b
->fields
.structure
[i
].memory_coherent
)
1172 if (this->fields
.structure
[i
].memory_volatile
1173 != b
->fields
.structure
[i
].memory_volatile
)
1175 if (this->fields
.structure
[i
].memory_restrict
1176 != b
->fields
.structure
[i
].memory_restrict
)
1178 if (this->fields
.structure
[i
].image_format
1179 != b
->fields
.structure
[i
].image_format
)
1181 if (match_precision
&&
1182 this->fields
.structure
[i
].precision
1183 != b
->fields
.structure
[i
].precision
)
1185 if (this->fields
.structure
[i
].explicit_xfb_buffer
1186 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
1188 if (this->fields
.structure
[i
].xfb_buffer
1189 != b
->fields
.structure
[i
].xfb_buffer
)
1191 if (this->fields
.structure
[i
].xfb_stride
1192 != b
->fields
.structure
[i
].xfb_stride
)
1201 glsl_type::record_key_compare(const void *a
, const void *b
)
1203 const glsl_type
*const key1
= (glsl_type
*) a
;
1204 const glsl_type
*const key2
= (glsl_type
*) b
;
1206 return strcmp(key1
->name
, key2
->name
) == 0 &&
1207 key1
->record_compare(key2
, true);
1212 * Generate an integer hash value for a glsl_type structure type.
1215 glsl_type::record_key_hash(const void *a
)
1217 const glsl_type
*const key
= (glsl_type
*) a
;
1218 uintptr_t hash
= key
->length
;
1221 for (unsigned i
= 0; i
< key
->length
; i
++) {
1222 /* casting pointer to uintptr_t */
1223 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
1226 if (sizeof(hash
) == 8)
1227 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
1236 glsl_type::get_struct_instance(const glsl_struct_field
*fields
,
1237 unsigned num_fields
,
1241 const glsl_type
key(fields
, num_fields
, name
, packed
);
1243 mtx_lock(&glsl_type::hash_mutex
);
1244 assert(glsl_type_users
> 0);
1246 if (struct_types
== NULL
) {
1247 struct_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1248 record_key_compare
);
1251 const struct hash_entry
*entry
= _mesa_hash_table_search(struct_types
,
1253 if (entry
== NULL
) {
1254 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
, packed
);
1256 entry
= _mesa_hash_table_insert(struct_types
, t
, (void *) t
);
1259 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1260 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1261 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1262 assert(((glsl_type
*) entry
->data
)->packed
== packed
);
1264 glsl_type
*t
= (glsl_type
*) entry
->data
;
1266 mtx_unlock(&glsl_type::hash_mutex
);
1273 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1274 unsigned num_fields
,
1275 enum glsl_interface_packing packing
,
1277 const char *block_name
)
1279 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1281 mtx_lock(&glsl_type::hash_mutex
);
1282 assert(glsl_type_users
> 0);
1284 if (interface_types
== NULL
) {
1285 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1286 record_key_compare
);
1289 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1291 if (entry
== NULL
) {
1292 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1293 packing
, row_major
, block_name
);
1295 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1298 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1299 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1300 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1302 glsl_type
*t
= (glsl_type
*) entry
->data
;
1304 mtx_unlock(&glsl_type::hash_mutex
);
1310 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1312 const glsl_type
key(subroutine_name
);
1314 mtx_lock(&glsl_type::hash_mutex
);
1315 assert(glsl_type_users
> 0);
1317 if (subroutine_types
== NULL
) {
1318 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1319 record_key_compare
);
1322 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1324 if (entry
== NULL
) {
1325 const glsl_type
*t
= new glsl_type(subroutine_name
);
1327 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1330 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1331 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1333 glsl_type
*t
= (glsl_type
*) entry
->data
;
1335 mtx_unlock(&glsl_type::hash_mutex
);
1342 function_key_compare(const void *a
, const void *b
)
1344 const glsl_type
*const key1
= (glsl_type
*) a
;
1345 const glsl_type
*const key2
= (glsl_type
*) b
;
1347 if (key1
->length
!= key2
->length
)
1350 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1351 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1356 function_key_hash(const void *a
)
1358 const glsl_type
*const key
= (glsl_type
*) a
;
1359 return _mesa_hash_data(key
->fields
.parameters
,
1360 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1364 glsl_type::get_function_instance(const glsl_type
*return_type
,
1365 const glsl_function_param
*params
,
1366 unsigned num_params
)
1368 const glsl_type
key(return_type
, params
, num_params
);
1370 mtx_lock(&glsl_type::hash_mutex
);
1371 assert(glsl_type_users
> 0);
1373 if (function_types
== NULL
) {
1374 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1375 function_key_compare
);
1378 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1379 if (entry
== NULL
) {
1380 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1382 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1385 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1387 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1388 assert(t
->length
== num_params
);
1390 mtx_unlock(&glsl_type::hash_mutex
);
1397 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1399 if (type_a
->is_matrix() && type_b
->is_matrix()) {
1400 /* Matrix multiply. The columns of A must match the rows of B. Given
1401 * the other previously tested constraints, this means the vector type
1402 * of a row from A must be the same as the vector type of a column from
1405 if (type_a
->row_type() == type_b
->column_type()) {
1406 /* The resulting matrix has the number of columns of matrix B and
1407 * the number of rows of matrix A. We get the row count of A by
1408 * looking at the size of a vector that makes up a column. The
1409 * transpose (size of a row) is done for B.
1411 const glsl_type
*const type
=
1412 get_instance(type_a
->base_type
,
1413 type_a
->column_type()->vector_elements
,
1414 type_b
->row_type()->vector_elements
);
1415 assert(type
!= error_type
);
1419 } else if (type_a
== type_b
) {
1421 } else if (type_a
->is_matrix()) {
1422 /* A is a matrix and B is a column vector. Columns of A must match
1423 * rows of B. Given the other previously tested constraints, this
1424 * means the vector type of a row from A must be the same as the
1425 * vector the type of B.
1427 if (type_a
->row_type() == type_b
) {
1428 /* The resulting vector has a number of elements equal to
1429 * the number of rows of matrix A. */
1430 const glsl_type
*const type
=
1431 get_instance(type_a
->base_type
,
1432 type_a
->column_type()->vector_elements
,
1434 assert(type
!= error_type
);
1439 assert(type_b
->is_matrix());
1441 /* A is a row vector and B is a matrix. Columns of A must match rows
1442 * of B. Given the other previously tested constraints, this means
1443 * the type of A must be the same as the vector type of a column from
1446 if (type_a
== type_b
->column_type()) {
1447 /* The resulting vector has a number of elements equal to
1448 * the number of columns of matrix B. */
1449 const glsl_type
*const type
=
1450 get_instance(type_a
->base_type
,
1451 type_b
->row_type()->vector_elements
,
1453 assert(type
!= error_type
);
1464 glsl_type::field_type(const char *name
) const
1466 if (this->base_type
!= GLSL_TYPE_STRUCT
1467 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1470 for (unsigned i
= 0; i
< this->length
; i
++) {
1471 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1472 return this->fields
.structure
[i
].type
;
1480 glsl_type::field_index(const char *name
) const
1482 if (this->base_type
!= GLSL_TYPE_STRUCT
1483 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1486 for (unsigned i
= 0; i
< this->length
; i
++) {
1487 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1496 glsl_type::component_slots() const
1498 switch (this->base_type
) {
1499 case GLSL_TYPE_UINT
:
1501 case GLSL_TYPE_UINT8
:
1502 case GLSL_TYPE_INT8
:
1503 case GLSL_TYPE_UINT16
:
1504 case GLSL_TYPE_INT16
:
1505 case GLSL_TYPE_FLOAT
:
1506 case GLSL_TYPE_FLOAT16
:
1507 case GLSL_TYPE_BOOL
:
1508 return this->components();
1510 case GLSL_TYPE_DOUBLE
:
1511 case GLSL_TYPE_UINT64
:
1512 case GLSL_TYPE_INT64
:
1513 return 2 * this->components();
1515 case GLSL_TYPE_STRUCT
:
1516 case GLSL_TYPE_INTERFACE
: {
1519 for (unsigned i
= 0; i
< this->length
; i
++)
1520 size
+= this->fields
.structure
[i
].type
->component_slots();
1525 case GLSL_TYPE_ARRAY
:
1526 return this->length
* this->fields
.array
->component_slots();
1528 case GLSL_TYPE_SAMPLER
:
1529 case GLSL_TYPE_IMAGE
:
1532 case GLSL_TYPE_SUBROUTINE
:
1535 case GLSL_TYPE_FUNCTION
:
1536 case GLSL_TYPE_ATOMIC_UINT
:
1537 case GLSL_TYPE_VOID
:
1538 case GLSL_TYPE_ERROR
:
1546 glsl_type::struct_location_offset(unsigned length
) const
1548 unsigned offset
= 0;
1549 const glsl_type
*t
= this->without_array();
1550 if (t
->is_struct()) {
1551 assert(length
<= t
->length
);
1553 for (unsigned i
= 0; i
< length
; i
++) {
1554 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1555 const glsl_type
*wa
= st
->without_array();
1556 if (wa
->is_struct()) {
1557 unsigned r_offset
= wa
->struct_location_offset(wa
->length
);
1558 offset
+= st
->is_array() ?
1559 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1560 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1561 unsigned outer_array_size
= st
->length
;
1562 const glsl_type
*base_type
= st
->fields
.array
;
1564 /* For arrays of arrays the outer arrays take up a uniform
1565 * slot for each element. The innermost array elements share a
1566 * single slot so we ignore the innermost array when calculating
1569 while (base_type
->fields
.array
->is_array()) {
1570 outer_array_size
= outer_array_size
* base_type
->length
;
1571 base_type
= base_type
->fields
.array
;
1573 offset
+= outer_array_size
;
1575 /* We dont worry about arrays here because unless the array
1576 * contains a structure or another array it only takes up a single
1587 glsl_type::uniform_locations() const
1591 switch (this->base_type
) {
1592 case GLSL_TYPE_UINT
:
1594 case GLSL_TYPE_FLOAT
:
1595 case GLSL_TYPE_FLOAT16
:
1596 case GLSL_TYPE_DOUBLE
:
1597 case GLSL_TYPE_UINT16
:
1598 case GLSL_TYPE_UINT8
:
1599 case GLSL_TYPE_INT16
:
1600 case GLSL_TYPE_INT8
:
1601 case GLSL_TYPE_UINT64
:
1602 case GLSL_TYPE_INT64
:
1603 case GLSL_TYPE_BOOL
:
1604 case GLSL_TYPE_SAMPLER
:
1605 case GLSL_TYPE_IMAGE
:
1606 case GLSL_TYPE_SUBROUTINE
:
1609 case GLSL_TYPE_STRUCT
:
1610 case GLSL_TYPE_INTERFACE
:
1611 for (unsigned i
= 0; i
< this->length
; i
++)
1612 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1614 case GLSL_TYPE_ARRAY
:
1615 return this->length
* this->fields
.array
->uniform_locations();
1622 glsl_type::varying_count() const
1626 switch (this->base_type
) {
1627 case GLSL_TYPE_UINT
:
1629 case GLSL_TYPE_FLOAT
:
1630 case GLSL_TYPE_FLOAT16
:
1631 case GLSL_TYPE_DOUBLE
:
1632 case GLSL_TYPE_BOOL
:
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
:
1641 case GLSL_TYPE_STRUCT
:
1642 case GLSL_TYPE_INTERFACE
:
1643 for (unsigned i
= 0; i
< this->length
; i
++)
1644 size
+= this->fields
.structure
[i
].type
->varying_count();
1646 case GLSL_TYPE_ARRAY
:
1647 /* Don't count innermost array elements */
1648 if (this->without_array()->is_struct() ||
1649 this->without_array()->is_interface() ||
1650 this->fields
.array
->is_array())
1651 return this->length
* this->fields
.array
->varying_count();
1653 return this->fields
.array
->varying_count();
1655 assert(!"unsupported varying type");
1661 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1662 _mesa_glsl_parse_state
*state
) const
1664 if (this == desired
)
1667 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1668 * state, we're doing intra-stage function linking where these checks have
1669 * already been done.
1671 if (state
&& !state
->has_implicit_conversions())
1674 /* There is no conversion among matrix types. */
1675 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1678 /* Vector size must match. */
1679 if (this->vector_elements
!= desired
->vector_elements
)
1682 /* int and uint can be converted to float. */
1683 if (desired
->is_float() && this->is_integer_32())
1686 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1687 * can be converted to uint. Note that state may be NULL here, when
1688 * resolving function calls in the linker. By this time, all the
1689 * state-dependent checks have already happened though, so allow anything
1690 * that's allowed in any shader version.
1692 if ((!state
|| state
->has_implicit_int_to_uint_conversion()) &&
1693 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1696 /* No implicit conversions from double. */
1697 if ((!state
|| state
->has_double()) && this->is_double())
1700 /* Conversions from different types to double. */
1701 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1702 if (this->is_float())
1704 if (this->is_integer_32())
1712 glsl_type::std140_base_alignment(bool row_major
) const
1714 unsigned N
= is_64bit() ? 8 : 4;
1716 /* (1) If the member is a scalar consuming <N> basic machine units, the
1717 * base alignment is <N>.
1719 * (2) If the member is a two- or four-component vector with components
1720 * consuming <N> basic machine units, the base alignment is 2<N> or
1721 * 4<N>, respectively.
1723 * (3) If the member is a three-component vector with components consuming
1724 * <N> basic machine units, the base alignment is 4<N>.
1726 if (this->is_scalar() || this->is_vector()) {
1727 switch (this->vector_elements
) {
1738 /* (4) If the member is an array of scalars or vectors, the base alignment
1739 * and array stride are set to match the base alignment of a single
1740 * array element, according to rules (1), (2), and (3), and rounded up
1741 * to the base alignment of a vec4. The array may have padding at the
1742 * end; the base offset of the member following the array is rounded up
1743 * to the next multiple of the base alignment.
1745 * (6) If the member is an array of <S> column-major matrices with <C>
1746 * columns and <R> rows, the matrix is stored identically to a row of
1747 * <S>*<C> column vectors with <R> components each, according to rule
1750 * (8) If the member is an array of <S> row-major matrices with <C> columns
1751 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1752 * row vectors with <C> components each, according to rule (4).
1754 * (10) If the member is an array of <S> structures, the <S> elements of
1755 * the array are laid out in order, according to rule (9).
1757 if (this->is_array()) {
1758 if (this->fields
.array
->is_scalar() ||
1759 this->fields
.array
->is_vector() ||
1760 this->fields
.array
->is_matrix()) {
1761 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1763 assert(this->fields
.array
->is_struct() ||
1764 this->fields
.array
->is_array());
1765 return this->fields
.array
->std140_base_alignment(row_major
);
1769 /* (5) If the member is a column-major matrix with <C> columns and
1770 * <R> rows, the matrix is stored identically to an array of
1771 * <C> column vectors with <R> components each, according to
1774 * (7) If the member is a row-major matrix with <C> columns and <R>
1775 * rows, the matrix is stored identically to an array of <R>
1776 * row vectors with <C> components each, according to rule (4).
1778 if (this->is_matrix()) {
1779 const struct glsl_type
*vec_type
, *array_type
;
1780 int c
= this->matrix_columns
;
1781 int r
= this->vector_elements
;
1784 vec_type
= get_instance(base_type
, c
, 1);
1785 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1787 vec_type
= get_instance(base_type
, r
, 1);
1788 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1791 return array_type
->std140_base_alignment(false);
1794 /* (9) If the member is a structure, the base alignment of the
1795 * structure is <N>, where <N> is the largest base alignment
1796 * value of any of its members, and rounded up to the base
1797 * alignment of a vec4. The individual members of this
1798 * sub-structure are then assigned offsets by applying this set
1799 * of rules recursively, where the base offset of the first
1800 * member of the sub-structure is equal to the aligned offset
1801 * of the structure. The structure may have padding at the end;
1802 * the base offset of the member following the sub-structure is
1803 * rounded up to the next multiple of the base alignment of the
1806 if (this->is_struct()) {
1807 unsigned base_alignment
= 16;
1808 for (unsigned i
= 0; i
< this->length
; i
++) {
1809 bool field_row_major
= row_major
;
1810 const enum glsl_matrix_layout matrix_layout
=
1811 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1812 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1813 field_row_major
= true;
1814 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1815 field_row_major
= false;
1818 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1819 base_alignment
= MAX2(base_alignment
,
1820 field_type
->std140_base_alignment(field_row_major
));
1822 return base_alignment
;
1825 assert(!"not reached");
1830 glsl_type::std140_size(bool row_major
) const
1832 unsigned N
= is_64bit() ? 8 : 4;
1834 /* (1) If the member is a scalar consuming <N> basic machine units, the
1835 * base alignment is <N>.
1837 * (2) If the member is a two- or four-component vector with components
1838 * consuming <N> basic machine units, the base alignment is 2<N> or
1839 * 4<N>, respectively.
1841 * (3) If the member is a three-component vector with components consuming
1842 * <N> basic machine units, the base alignment is 4<N>.
1844 if (this->is_scalar() || this->is_vector()) {
1845 assert(this->explicit_stride
== 0);
1846 return this->vector_elements
* N
;
1849 /* (5) If the member is a column-major matrix with <C> columns and
1850 * <R> rows, the matrix is stored identically to an array of
1851 * <C> column vectors with <R> components each, according to
1854 * (6) If the member is an array of <S> column-major matrices with <C>
1855 * columns and <R> rows, the matrix is stored identically to a row of
1856 * <S>*<C> column vectors with <R> components each, according to rule
1859 * (7) If the member is a row-major matrix with <C> columns and <R>
1860 * rows, the matrix is stored identically to an array of <R>
1861 * row vectors with <C> components each, according to rule (4).
1863 * (8) If the member is an array of <S> row-major matrices with <C> columns
1864 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1865 * row vectors with <C> components each, according to rule (4).
1867 if (this->without_array()->is_matrix()) {
1868 const struct glsl_type
*element_type
;
1869 const struct glsl_type
*vec_type
;
1870 unsigned int array_len
;
1872 if (this->is_array()) {
1873 element_type
= this->without_array();
1874 array_len
= this->arrays_of_arrays_size();
1876 element_type
= this;
1881 vec_type
= get_instance(element_type
->base_type
,
1882 element_type
->matrix_columns
, 1);
1884 array_len
*= element_type
->vector_elements
;
1886 vec_type
= get_instance(element_type
->base_type
,
1887 element_type
->vector_elements
, 1);
1888 array_len
*= element_type
->matrix_columns
;
1890 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1893 return array_type
->std140_size(false);
1896 /* (4) If the member is an array of scalars or vectors, the base alignment
1897 * and array stride are set to match the base alignment of a single
1898 * array element, according to rules (1), (2), and (3), and rounded up
1899 * to the base alignment of a vec4. The array may have padding at the
1900 * end; the base offset of the member following the array is rounded up
1901 * to the next multiple of the base alignment.
1903 * (10) If the member is an array of <S> structures, the <S> elements of
1904 * the array are laid out in order, according to rule (9).
1906 if (this->is_array()) {
1908 if (this->without_array()->is_struct()) {
1909 stride
= this->without_array()->std140_size(row_major
);
1911 unsigned element_base_align
=
1912 this->without_array()->std140_base_alignment(row_major
);
1913 stride
= MAX2(element_base_align
, 16);
1916 unsigned size
= this->arrays_of_arrays_size() * stride
;
1917 assert(this->explicit_stride
== 0 ||
1918 size
== this->length
* this->explicit_stride
);
1922 /* (9) If the member is a structure, the base alignment of the
1923 * structure is <N>, where <N> is the largest base alignment
1924 * value of any of its members, and rounded up to the base
1925 * alignment of a vec4. The individual members of this
1926 * sub-structure are then assigned offsets by applying this set
1927 * of rules recursively, where the base offset of the first
1928 * member of the sub-structure is equal to the aligned offset
1929 * of the structure. The structure may have padding at the end;
1930 * the base offset of the member following the sub-structure is
1931 * rounded up to the next multiple of the base alignment of the
1934 if (this->is_struct() || this->is_interface()) {
1936 unsigned max_align
= 0;
1938 for (unsigned i
= 0; i
< this->length
; i
++) {
1939 bool field_row_major
= row_major
;
1940 const enum glsl_matrix_layout matrix_layout
=
1941 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1942 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1943 field_row_major
= true;
1944 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1945 field_row_major
= false;
1948 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1949 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1951 /* Ignore unsized arrays when calculating size */
1952 if (field_type
->is_unsized_array())
1955 size
= glsl_align(size
, align
);
1956 size
+= field_type
->std140_size(field_row_major
);
1958 max_align
= MAX2(align
, max_align
);
1960 if (field_type
->is_struct() && (i
+ 1 < this->length
))
1961 size
= glsl_align(size
, 16);
1963 size
= glsl_align(size
, MAX2(max_align
, 16));
1967 assert(!"not reached");
1972 glsl_type::get_explicit_std140_type(bool row_major
) const
1974 if (this->is_vector() || this->is_scalar()) {
1976 } else if (this->is_matrix()) {
1977 const glsl_type
*vec_type
;
1979 vec_type
= get_instance(this->base_type
, this->matrix_columns
, 1);
1981 vec_type
= get_instance(this->base_type
, this->vector_elements
, 1);
1982 unsigned elem_size
= vec_type
->std140_size(false);
1983 unsigned stride
= glsl_align(elem_size
, 16);
1984 return get_instance(this->base_type
, this->vector_elements
,
1985 this->matrix_columns
, stride
, row_major
);
1986 } else if (this->is_array()) {
1987 unsigned elem_size
= this->fields
.array
->std140_size(row_major
);
1988 const glsl_type
*elem_type
=
1989 this->fields
.array
->get_explicit_std140_type(row_major
);
1990 unsigned stride
= glsl_align(elem_size
, 16);
1991 return get_array_instance(elem_type
, this->length
, stride
);
1992 } else if (this->is_struct() || this->is_interface()) {
1993 glsl_struct_field
*fields
= new glsl_struct_field
[this->length
];
1994 unsigned offset
= 0;
1995 for (unsigned i
= 0; i
< length
; i
++) {
1996 fields
[i
] = this->fields
.structure
[i
];
1998 bool field_row_major
= row_major
;
1999 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2000 field_row_major
= false;
2001 } else if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2002 field_row_major
= true;
2005 fields
[i
].type
->get_explicit_std140_type(field_row_major
);
2007 unsigned fsize
= fields
[i
].type
->std140_size(field_row_major
);
2008 unsigned falign
= fields
[i
].type
->std140_base_alignment(field_row_major
);
2009 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2010 * Layout Qualifiers":
2012 * "The actual offset of a member is computed as follows: If
2013 * offset was declared, start with that offset, otherwise start
2014 * with the next available offset. If the resulting offset is not
2015 * a multiple of the actual alignment, increase it to the first
2016 * offset that is a multiple of the actual alignment. This results
2017 * in the actual offset the member will have."
2019 if (fields
[i
].offset
>= 0) {
2020 assert((unsigned)fields
[i
].offset
>= offset
);
2021 offset
= fields
[i
].offset
;
2023 offset
= glsl_align(offset
, falign
);
2024 fields
[i
].offset
= offset
;
2028 const glsl_type
*type
;
2029 if (this->is_struct())
2030 type
= get_struct_instance(fields
, this->length
, this->name
);
2032 type
= get_interface_instance(fields
, this->length
,
2033 (enum glsl_interface_packing
)this->interface_packing
,
2034 this->interface_row_major
,
2040 unreachable("Invalid type for UBO or SSBO");
2045 glsl_type::std430_base_alignment(bool row_major
) const
2048 unsigned N
= is_64bit() ? 8 : 4;
2050 /* (1) If the member is a scalar consuming <N> basic machine units, the
2051 * base alignment is <N>.
2053 * (2) If the member is a two- or four-component vector with components
2054 * consuming <N> basic machine units, the base alignment is 2<N> or
2055 * 4<N>, respectively.
2057 * (3) If the member is a three-component vector with components consuming
2058 * <N> basic machine units, the base alignment is 4<N>.
2060 if (this->is_scalar() || this->is_vector()) {
2061 switch (this->vector_elements
) {
2072 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2074 * "When using the std430 storage layout, shader storage blocks will be
2075 * laid out in buffer storage identically to uniform and shader storage
2076 * blocks using the std140 layout, except that the base alignment and
2077 * stride of arrays of scalars and vectors in rule 4 and of structures
2078 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2081 /* (1) If the member is a scalar consuming <N> basic machine units, the
2082 * base alignment is <N>.
2084 * (2) If the member is a two- or four-component vector with components
2085 * consuming <N> basic machine units, the base alignment is 2<N> or
2086 * 4<N>, respectively.
2088 * (3) If the member is a three-component vector with components consuming
2089 * <N> basic machine units, the base alignment is 4<N>.
2091 if (this->is_array())
2092 return this->fields
.array
->std430_base_alignment(row_major
);
2094 /* (5) If the member is a column-major matrix with <C> columns and
2095 * <R> rows, the matrix is stored identically to an array of
2096 * <C> column vectors with <R> components each, according to
2099 * (7) If the member is a row-major matrix with <C> columns and <R>
2100 * rows, the matrix is stored identically to an array of <R>
2101 * row vectors with <C> components each, according to rule (4).
2103 if (this->is_matrix()) {
2104 const struct glsl_type
*vec_type
, *array_type
;
2105 int c
= this->matrix_columns
;
2106 int r
= this->vector_elements
;
2109 vec_type
= get_instance(base_type
, c
, 1);
2110 array_type
= glsl_type::get_array_instance(vec_type
, r
);
2112 vec_type
= get_instance(base_type
, r
, 1);
2113 array_type
= glsl_type::get_array_instance(vec_type
, c
);
2116 return array_type
->std430_base_alignment(false);
2119 /* (9) If the member is a structure, the base alignment of the
2120 * structure is <N>, where <N> is the largest base alignment
2121 * value of any of its members, and rounded up to the base
2122 * alignment of a vec4. The individual members of this
2123 * sub-structure are then assigned offsets by applying this set
2124 * of rules recursively, where the base offset of the first
2125 * member of the sub-structure is equal to the aligned offset
2126 * of the structure. The structure may have padding at the end;
2127 * the base offset of the member following the sub-structure is
2128 * rounded up to the next multiple of the base alignment of the
2131 if (this->is_struct()) {
2132 unsigned base_alignment
= 0;
2133 for (unsigned i
= 0; i
< this->length
; i
++) {
2134 bool field_row_major
= row_major
;
2135 const enum glsl_matrix_layout matrix_layout
=
2136 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
2137 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2138 field_row_major
= true;
2139 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2140 field_row_major
= false;
2143 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
2144 base_alignment
= MAX2(base_alignment
,
2145 field_type
->std430_base_alignment(field_row_major
));
2147 assert(base_alignment
> 0);
2148 return base_alignment
;
2150 assert(!"not reached");
2155 glsl_type::std430_array_stride(bool row_major
) const
2157 unsigned N
= is_64bit() ? 8 : 4;
2159 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
2160 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
2162 * (3) If the member is a three-component vector with components consuming
2163 * <N> basic machine units, the base alignment is 4<N>.
2165 if (this->is_vector() && this->vector_elements
== 3)
2168 /* By default use std430_size(row_major) */
2169 unsigned stride
= this->std430_size(row_major
);
2170 assert(this->explicit_stride
== 0 || this->explicit_stride
== stride
);
2174 /* Note that the value returned by this method is only correct if the
2175 * explit offset, and stride values are set, so only with SPIR-V shaders.
2176 * Should not be used with GLSL shaders.
2180 glsl_type::explicit_size(bool align_to_stride
) const
2182 if (this->is_struct() || this->is_interface()) {
2183 if (this->length
> 0) {
2186 for (unsigned i
= 0; i
< this->length
; i
++) {
2187 assert(this->fields
.structure
[i
].offset
>= 0);
2188 unsigned last_byte
= this->fields
.structure
[i
].offset
+
2189 this->fields
.structure
[i
].type
->explicit_size();
2190 size
= MAX2(size
, last_byte
);
2197 } else if (this->is_array()) {
2198 /* From ARB_program_interface_query spec:
2200 * "For the property of BUFFER_DATA_SIZE, then the implementation-dependent
2201 * minimum total buffer object size, in basic machine units, required to
2202 * hold all active variables associated with an active uniform block, shader
2203 * storage block, or atomic counter buffer is written to <params>. If the
2204 * final member of an active shader storage block is array with no declared
2205 * size, the minimum buffer size is computed assuming the array was declared
2206 * as an array with one element."
2209 if (this->is_unsized_array())
2210 return this->explicit_stride
;
2212 assert(this->length
> 0);
2213 unsigned elem_size
= align_to_stride
? this->explicit_stride
: this->fields
.array
->explicit_size();
2214 assert(this->explicit_stride
>= elem_size
);
2216 return this->explicit_stride
* (this->length
- 1) + elem_size
;
2217 } else if (this->is_matrix()) {
2218 const struct glsl_type
*elem_type
;
2221 if (this->interface_row_major
) {
2222 elem_type
= get_instance(this->base_type
,
2223 this->matrix_columns
, 1);
2224 length
= this->vector_elements
;
2226 elem_type
= get_instance(this->base_type
,
2227 this->vector_elements
, 1);
2228 length
= this->matrix_columns
;
2231 unsigned elem_size
= align_to_stride
? this->explicit_stride
: elem_type
->explicit_size();
2233 assert(this->explicit_stride
);
2234 return this->explicit_stride
* (length
- 1) + elem_size
;
2237 unsigned N
= this->bit_size() / 8;
2239 return this->vector_elements
* N
;
2243 glsl_type::std430_size(bool row_major
) const
2245 unsigned N
= is_64bit() ? 8 : 4;
2247 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2249 * "When using the std430 storage layout, shader storage blocks will be
2250 * laid out in buffer storage identically to uniform and shader storage
2251 * blocks using the std140 layout, except that the base alignment and
2252 * stride of arrays of scalars and vectors in rule 4 and of structures
2253 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2255 if (this->is_scalar() || this->is_vector()) {
2256 assert(this->explicit_stride
== 0);
2257 return this->vector_elements
* N
;
2260 if (this->without_array()->is_matrix()) {
2261 const struct glsl_type
*element_type
;
2262 const struct glsl_type
*vec_type
;
2263 unsigned int array_len
;
2265 if (this->is_array()) {
2266 element_type
= this->without_array();
2267 array_len
= this->arrays_of_arrays_size();
2269 element_type
= this;
2274 vec_type
= get_instance(element_type
->base_type
,
2275 element_type
->matrix_columns
, 1);
2277 array_len
*= element_type
->vector_elements
;
2279 vec_type
= get_instance(element_type
->base_type
,
2280 element_type
->vector_elements
, 1);
2281 array_len
*= element_type
->matrix_columns
;
2283 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
2286 return array_type
->std430_size(false);
2289 if (this->is_array()) {
2291 if (this->without_array()->is_struct())
2292 stride
= this->without_array()->std430_size(row_major
);
2294 stride
= this->without_array()->std430_base_alignment(row_major
);
2296 unsigned size
= this->arrays_of_arrays_size() * stride
;
2297 assert(this->explicit_stride
== 0 ||
2298 size
== this->length
* this->explicit_stride
);
2302 if (this->is_struct() || this->is_interface()) {
2304 unsigned max_align
= 0;
2306 for (unsigned i
= 0; i
< this->length
; i
++) {
2307 bool field_row_major
= row_major
;
2308 const enum glsl_matrix_layout matrix_layout
=
2309 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
2310 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2311 field_row_major
= true;
2312 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2313 field_row_major
= false;
2316 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
2317 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
2318 size
= glsl_align(size
, align
);
2319 size
+= field_type
->std430_size(field_row_major
);
2321 max_align
= MAX2(align
, max_align
);
2323 size
= glsl_align(size
, max_align
);
2327 assert(!"not reached");
2332 glsl_type::get_explicit_std430_type(bool row_major
) const
2334 if (this->is_vector() || this->is_scalar()) {
2336 } else if (this->is_matrix()) {
2337 const glsl_type
*vec_type
;
2339 vec_type
= get_instance(this->base_type
, this->matrix_columns
, 1);
2341 vec_type
= get_instance(this->base_type
, this->vector_elements
, 1);
2342 unsigned stride
= vec_type
->std430_array_stride(false);
2343 return get_instance(this->base_type
, this->vector_elements
,
2344 this->matrix_columns
, stride
, row_major
);
2345 } else if (this->is_array()) {
2346 const glsl_type
*elem_type
=
2347 this->fields
.array
->get_explicit_std430_type(row_major
);
2348 unsigned stride
= this->fields
.array
->std430_array_stride(row_major
);
2349 return get_array_instance(elem_type
, this->length
, stride
);
2350 } else if (this->is_struct() || this->is_interface()) {
2351 glsl_struct_field
*fields
= new glsl_struct_field
[this->length
];
2352 unsigned offset
= 0;
2353 for (unsigned i
= 0; i
< length
; i
++) {
2354 fields
[i
] = this->fields
.structure
[i
];
2356 bool field_row_major
= row_major
;
2357 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2358 field_row_major
= false;
2359 } else if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2360 field_row_major
= true;
2363 fields
[i
].type
->get_explicit_std430_type(field_row_major
);
2365 unsigned fsize
= fields
[i
].type
->std430_size(field_row_major
);
2366 unsigned falign
= fields
[i
].type
->std430_base_alignment(field_row_major
);
2367 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2368 * Layout Qualifiers":
2370 * "The actual offset of a member is computed as follows: If
2371 * offset was declared, start with that offset, otherwise start
2372 * with the next available offset. If the resulting offset is not
2373 * a multiple of the actual alignment, increase it to the first
2374 * offset that is a multiple of the actual alignment. This results
2375 * in the actual offset the member will have."
2377 if (fields
[i
].offset
>= 0) {
2378 assert((unsigned)fields
[i
].offset
>= offset
);
2379 offset
= fields
[i
].offset
;
2381 offset
= glsl_align(offset
, falign
);
2382 fields
[i
].offset
= offset
;
2386 const glsl_type
*type
;
2387 if (this->is_struct())
2388 type
= get_struct_instance(fields
, this->length
, this->name
);
2390 type
= get_interface_instance(fields
, this->length
,
2391 (enum glsl_interface_packing
)this->interface_packing
,
2392 this->interface_row_major
,
2398 unreachable("Invalid type for SSBO");
2403 glsl_type::get_explicit_interface_type(bool supports_std430
) const
2405 enum glsl_interface_packing packing
=
2406 this->get_internal_ifc_packing(supports_std430
);
2407 if (packing
== GLSL_INTERFACE_PACKING_STD140
) {
2408 return this->get_explicit_std140_type(this->interface_row_major
);
2410 assert(packing
== GLSL_INTERFACE_PACKING_STD430
);
2411 return this->get_explicit_std430_type(this->interface_row_major
);
2415 /* This differs from get_explicit_std430_type() in that it:
2416 * - can size arrays slightly smaller ("stride * (len - 1) + elem_size" instead
2417 * of "stride * len")
2418 * - consumes a glsl_type_size_align_func which allows 8 and 16-bit values to be
2419 * packed more tightly
2420 * - overrides any struct field offsets but get_explicit_std430_type() tries to
2421 * respect any existing ones
2424 glsl_type::get_explicit_type_for_size_align(glsl_type_size_align_func type_info
,
2425 unsigned *size
, unsigned *alignment
) const
2427 if (this->is_scalar() || this->is_vector()) {
2428 type_info(this, size
, alignment
);
2430 } else if (this->is_array()) {
2431 unsigned elem_size
, elem_align
;
2432 const struct glsl_type
*explicit_element
=
2433 this->fields
.array
->get_explicit_type_for_size_align(type_info
, &elem_size
, &elem_align
);
2435 unsigned stride
= align(elem_size
, elem_align
);
2437 *size
= stride
* (this->length
- 1) + elem_size
;
2438 *alignment
= elem_align
;
2439 return glsl_type::get_array_instance(explicit_element
, this->length
, stride
);
2440 } else if (this->is_struct()) {
2441 struct glsl_struct_field
*fields
= (struct glsl_struct_field
*)
2442 malloc(sizeof(struct glsl_struct_field
) * this->length
);
2446 for (unsigned i
= 0; i
< this->length
; i
++) {
2447 fields
[i
] = this->fields
.structure
[i
];
2448 assert(fields
[i
].matrix_layout
!= GLSL_MATRIX_LAYOUT_ROW_MAJOR
);
2450 unsigned field_size
, field_align
;
2452 fields
[i
].type
->get_explicit_type_for_size_align(type_info
, &field_size
, &field_align
);
2453 fields
[i
].offset
= align(*size
, field_align
);
2455 *size
= fields
[i
].offset
+ field_size
;
2456 *alignment
= MAX2(*alignment
, field_align
);
2459 const glsl_type
*type
= glsl_type::get_struct_instance(fields
, this->length
, this->name
, false);
2462 } else if (this->is_matrix()) {
2463 unsigned col_size
, col_align
;
2464 type_info(this->column_type(), &col_size
, &col_align
);
2465 unsigned stride
= align(col_size
, col_align
);
2467 *size
= this->matrix_columns
* stride
;
2468 *alignment
= col_align
;
2469 return glsl_type::get_instance(this->base_type
, this->vector_elements
,
2470 this->matrix_columns
, stride
, false);
2472 unreachable("Unhandled type.");
2477 glsl_type::count_vec4_slots(bool is_gl_vertex_input
, bool is_bindless
) const
2479 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2481 * "A scalar input counts the same amount against this limit as a vec4,
2482 * so applications may want to consider packing groups of four
2483 * unrelated float inputs together into a vector to better utilize the
2484 * capabilities of the underlying hardware. A matrix input will use up
2485 * multiple locations. The number of locations used will equal the
2486 * number of columns in the matrix."
2488 * The spec does not explicitly say how arrays are counted. However, it
2489 * should be safe to assume the total number of slots consumed by an array
2490 * is the number of entries in the array multiplied by the number of slots
2491 * consumed by a single element of the array.
2493 * The spec says nothing about how structs are counted, because vertex
2494 * attributes are not allowed to be (or contain) structs. However, Mesa
2495 * allows varying structs, the number of varying slots taken up by a
2496 * varying struct is simply equal to the sum of the number of slots taken
2497 * up by each element.
2499 * Doubles are counted different depending on whether they are vertex
2500 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
2501 * take one location no matter what size they are, otherwise dvec3/4
2502 * take two locations.
2504 switch (this->base_type
) {
2505 case GLSL_TYPE_UINT
:
2507 case GLSL_TYPE_UINT8
:
2508 case GLSL_TYPE_INT8
:
2509 case GLSL_TYPE_UINT16
:
2510 case GLSL_TYPE_INT16
:
2511 case GLSL_TYPE_FLOAT
:
2512 case GLSL_TYPE_FLOAT16
:
2513 case GLSL_TYPE_BOOL
:
2514 return this->matrix_columns
;
2515 case GLSL_TYPE_DOUBLE
:
2516 case GLSL_TYPE_UINT64
:
2517 case GLSL_TYPE_INT64
:
2518 if (this->vector_elements
> 2 && !is_gl_vertex_input
)
2519 return this->matrix_columns
* 2;
2521 return this->matrix_columns
;
2522 case GLSL_TYPE_STRUCT
:
2523 case GLSL_TYPE_INTERFACE
: {
2526 for (unsigned i
= 0; i
< this->length
; i
++) {
2527 const glsl_type
*member_type
= this->fields
.structure
[i
].type
;
2528 size
+= member_type
->count_vec4_slots(is_gl_vertex_input
, is_bindless
);
2534 case GLSL_TYPE_ARRAY
: {
2535 const glsl_type
*element
= this->fields
.array
;
2536 return this->length
* element
->count_vec4_slots(is_gl_vertex_input
,
2540 case GLSL_TYPE_SAMPLER
:
2541 case GLSL_TYPE_IMAGE
:
2547 case GLSL_TYPE_SUBROUTINE
:
2550 case GLSL_TYPE_FUNCTION
:
2551 case GLSL_TYPE_ATOMIC_UINT
:
2552 case GLSL_TYPE_VOID
:
2553 case GLSL_TYPE_ERROR
:
2557 assert(!"Unexpected type in count_attribute_slots()");
2563 glsl_type::count_dword_slots(bool is_bindless
) const
2565 switch (this->base_type
) {
2566 case GLSL_TYPE_UINT
:
2568 case GLSL_TYPE_FLOAT
:
2569 case GLSL_TYPE_BOOL
:
2570 return this->components();
2571 case GLSL_TYPE_UINT16
:
2572 case GLSL_TYPE_INT16
:
2573 case GLSL_TYPE_FLOAT16
:
2574 return DIV_ROUND_UP(this->components(), 2);
2575 case GLSL_TYPE_UINT8
:
2576 case GLSL_TYPE_INT8
:
2577 return DIV_ROUND_UP(this->components(), 4);
2578 case GLSL_TYPE_IMAGE
:
2579 case GLSL_TYPE_SAMPLER
:
2583 case GLSL_TYPE_DOUBLE
:
2584 case GLSL_TYPE_UINT64
:
2585 case GLSL_TYPE_INT64
:
2586 return this->components() * 2;
2587 case GLSL_TYPE_ARRAY
:
2588 return this->fields
.array
->count_dword_slots(is_bindless
) *
2591 case GLSL_TYPE_INTERFACE
:
2592 case GLSL_TYPE_STRUCT
: {
2594 for (unsigned i
= 0; i
< this->length
; i
++) {
2595 size
+= this->fields
.structure
[i
].type
->count_dword_slots(is_bindless
);
2600 case GLSL_TYPE_ATOMIC_UINT
:
2602 case GLSL_TYPE_SUBROUTINE
:
2604 case GLSL_TYPE_VOID
:
2605 case GLSL_TYPE_ERROR
:
2606 case GLSL_TYPE_FUNCTION
:
2608 unreachable("invalid type in st_glsl_type_dword_size()");
2615 glsl_type::coordinate_components() const
2617 enum glsl_sampler_dim dim
= (enum glsl_sampler_dim
)sampler_dimensionality
;
2618 int size
= glsl_get_sampler_dim_coordinate_components(dim
);
2620 /* Array textures need an additional component for the array index, except
2621 * for cubemap array images that behave like a 2D array of interleaved
2624 if (sampler_array
&&
2625 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2632 * Declarations of type flyweights (glsl_type::_foo_type) and
2633 * convenience pointers (glsl_type::foo_type).
2636 #define DECL_TYPE(NAME, ...) \
2637 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2638 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2640 #define STRUCT_TYPE(NAME)
2642 #include "compiler/builtin_type_macros.h"
2648 unsigned base_type
:5;
2649 unsigned interface_row_major
:1;
2650 unsigned vector_elements
:3;
2651 unsigned matrix_columns
:3;
2652 unsigned explicit_stride
:20;
2655 unsigned base_type
:5;
2656 unsigned dimensionality
:4;
2659 unsigned sampled_type
:2;
2663 unsigned base_type
:5;
2665 unsigned explicit_stride
:14;
2668 unsigned base_type
:5;
2669 unsigned interface_packing_or_packed
:2;
2670 unsigned interface_row_major
:1;
2676 encode_glsl_struct_field(blob
*blob
, const glsl_struct_field
*struct_field
)
2678 encode_type_to_blob(blob
, struct_field
->type
);
2679 blob_write_string(blob
, struct_field
->name
);
2680 blob_write_uint32(blob
, struct_field
->location
);
2681 blob_write_uint32(blob
, struct_field
->offset
);
2682 blob_write_uint32(blob
, struct_field
->xfb_buffer
);
2683 blob_write_uint32(blob
, struct_field
->xfb_stride
);
2684 blob_write_uint32(blob
, struct_field
->image_format
);
2685 blob_write_uint32(blob
, struct_field
->flags
);
2689 decode_glsl_struct_field_from_blob(blob_reader
*blob
, glsl_struct_field
*struct_field
)
2691 struct_field
->type
= decode_type_from_blob(blob
);
2692 struct_field
->name
= blob_read_string(blob
);
2693 struct_field
->location
= blob_read_uint32(blob
);
2694 struct_field
->offset
= blob_read_uint32(blob
);
2695 struct_field
->xfb_buffer
= blob_read_uint32(blob
);
2696 struct_field
->xfb_stride
= blob_read_uint32(blob
);
2697 struct_field
->image_format
= (pipe_format
)blob_read_uint32(blob
);
2698 struct_field
->flags
= blob_read_uint32(blob
);
2702 encode_type_to_blob(struct blob
*blob
, const glsl_type
*type
)
2705 blob_write_uint32(blob
, 0);
2709 STATIC_ASSERT(sizeof(union packed_type
) == 4);
2710 union packed_type encoded
;
2712 encoded
.basic
.base_type
= type
->base_type
;
2714 switch (type
->base_type
) {
2715 case GLSL_TYPE_UINT
:
2717 case GLSL_TYPE_FLOAT
:
2718 case GLSL_TYPE_FLOAT16
:
2719 case GLSL_TYPE_DOUBLE
:
2720 case GLSL_TYPE_UINT8
:
2721 case GLSL_TYPE_INT8
:
2722 case GLSL_TYPE_UINT16
:
2723 case GLSL_TYPE_INT16
:
2724 case GLSL_TYPE_UINT64
:
2725 case GLSL_TYPE_INT64
:
2726 case GLSL_TYPE_BOOL
:
2727 encoded
.basic
.interface_row_major
= type
->interface_row_major
;
2728 assert(type
->matrix_columns
< 8);
2729 if (type
->vector_elements
<= 4)
2730 encoded
.basic
.vector_elements
= type
->vector_elements
;
2731 else if (type
->vector_elements
== 8)
2732 encoded
.basic
.vector_elements
= 5;
2733 else if (type
->vector_elements
== 16)
2734 encoded
.basic
.vector_elements
= 6;
2735 encoded
.basic
.matrix_columns
= type
->matrix_columns
;
2736 encoded
.basic
.explicit_stride
= MIN2(type
->explicit_stride
, 0xfffff);
2737 blob_write_uint32(blob
, encoded
.u32
);
2738 /* If we don't have enough bits for explicit_stride, store it
2741 if (encoded
.basic
.explicit_stride
== 0xfffff)
2742 blob_write_uint32(blob
, type
->explicit_stride
);
2744 case GLSL_TYPE_SAMPLER
:
2745 encoded
.sampler
.dimensionality
= type
->sampler_dimensionality
;
2746 encoded
.sampler
.shadow
= type
->sampler_shadow
;
2747 encoded
.sampler
.array
= type
->sampler_array
;
2748 encoded
.sampler
.sampled_type
= type
->sampled_type
;
2750 case GLSL_TYPE_SUBROUTINE
:
2751 blob_write_uint32(blob
, encoded
.u32
);
2752 blob_write_string(blob
, type
->name
);
2754 case GLSL_TYPE_IMAGE
:
2755 encoded
.sampler
.dimensionality
= type
->sampler_dimensionality
;
2756 encoded
.sampler
.array
= type
->sampler_array
;
2757 encoded
.sampler
.sampled_type
= type
->sampled_type
;
2759 case GLSL_TYPE_ATOMIC_UINT
:
2761 case GLSL_TYPE_ARRAY
:
2762 encoded
.array
.length
= MIN2(type
->length
, 0x1fff);
2763 encoded
.array
.explicit_stride
= MIN2(type
->explicit_stride
, 0x3fff);
2764 blob_write_uint32(blob
, encoded
.u32
);
2765 /* If we don't have enough bits for length or explicit_stride, store it
2768 if (encoded
.array
.length
== 0x1fff)
2769 blob_write_uint32(blob
, type
->length
);
2770 if (encoded
.array
.explicit_stride
== 0x3fff)
2771 blob_write_uint32(blob
, type
->explicit_stride
);
2772 encode_type_to_blob(blob
, type
->fields
.array
);
2774 case GLSL_TYPE_STRUCT
:
2775 case GLSL_TYPE_INTERFACE
:
2776 encoded
.strct
.length
= MIN2(type
->length
, 0xffffff);
2777 if (type
->is_interface()) {
2778 encoded
.strct
.interface_packing_or_packed
= type
->interface_packing
;
2779 encoded
.strct
.interface_row_major
= type
->interface_row_major
;
2781 encoded
.strct
.interface_packing_or_packed
= type
->packed
;
2783 blob_write_uint32(blob
, encoded
.u32
);
2784 blob_write_string(blob
, type
->name
);
2786 /* If we don't have enough bits for length, store it separately. */
2787 if (encoded
.strct
.length
== 0xffffff)
2788 blob_write_uint32(blob
, type
->length
);
2790 for (unsigned i
= 0; i
< type
->length
; i
++)
2791 encode_glsl_struct_field(blob
, &type
->fields
.structure
[i
]);
2793 case GLSL_TYPE_VOID
:
2795 case GLSL_TYPE_ERROR
:
2797 assert(!"Cannot encode type!");
2802 blob_write_uint32(blob
, encoded
.u32
);
2806 decode_type_from_blob(struct blob_reader
*blob
)
2808 union packed_type encoded
;
2809 encoded
.u32
= blob_read_uint32(blob
);
2811 if (encoded
.u32
== 0) {
2815 glsl_base_type base_type
= (glsl_base_type
)encoded
.basic
.base_type
;
2817 switch (base_type
) {
2818 case GLSL_TYPE_UINT
:
2820 case GLSL_TYPE_FLOAT
:
2821 case GLSL_TYPE_FLOAT16
:
2822 case GLSL_TYPE_DOUBLE
:
2823 case GLSL_TYPE_UINT8
:
2824 case GLSL_TYPE_INT8
:
2825 case GLSL_TYPE_UINT16
:
2826 case GLSL_TYPE_INT16
:
2827 case GLSL_TYPE_UINT64
:
2828 case GLSL_TYPE_INT64
:
2829 case GLSL_TYPE_BOOL
: {
2830 unsigned explicit_stride
= encoded
.basic
.explicit_stride
;
2831 if (explicit_stride
== 0xfffff)
2832 explicit_stride
= blob_read_uint32(blob
);
2833 uint32_t vector_elements
= encoded
.basic
.vector_elements
;
2834 if (vector_elements
== 5)
2835 vector_elements
= 8;
2836 else if (vector_elements
== 6)
2837 vector_elements
= 16;
2838 return glsl_type::get_instance(base_type
, encoded
.basic
.vector_elements
,
2839 encoded
.basic
.matrix_columns
,
2841 encoded
.basic
.interface_row_major
);
2843 case GLSL_TYPE_SAMPLER
:
2844 return glsl_type::get_sampler_instance((enum glsl_sampler_dim
)encoded
.sampler
.dimensionality
,
2845 encoded
.sampler
.shadow
,
2846 encoded
.sampler
.array
,
2847 (glsl_base_type
) encoded
.sampler
.sampled_type
);
2848 case GLSL_TYPE_SUBROUTINE
:
2849 return glsl_type::get_subroutine_instance(blob_read_string(blob
));
2850 case GLSL_TYPE_IMAGE
:
2851 return glsl_type::get_image_instance((enum glsl_sampler_dim
)encoded
.sampler
.dimensionality
,
2852 encoded
.sampler
.array
,
2853 (glsl_base_type
) encoded
.sampler
.sampled_type
);
2854 case GLSL_TYPE_ATOMIC_UINT
:
2855 return glsl_type::atomic_uint_type
;
2856 case GLSL_TYPE_ARRAY
: {
2857 unsigned length
= encoded
.array
.length
;
2858 if (length
== 0x1fff)
2859 length
= blob_read_uint32(blob
);
2860 unsigned explicit_stride
= encoded
.array
.explicit_stride
;
2861 if (explicit_stride
== 0x3fff)
2862 explicit_stride
= blob_read_uint32(blob
);
2863 return glsl_type::get_array_instance(decode_type_from_blob(blob
),
2864 length
, explicit_stride
);
2866 case GLSL_TYPE_STRUCT
:
2867 case GLSL_TYPE_INTERFACE
: {
2868 char *name
= blob_read_string(blob
);
2869 unsigned num_fields
= encoded
.strct
.length
;
2870 if (num_fields
== 0xffffff)
2871 num_fields
= blob_read_uint32(blob
);
2873 glsl_struct_field
*fields
=
2874 (glsl_struct_field
*) malloc(sizeof(glsl_struct_field
) * num_fields
);
2875 for (unsigned i
= 0; i
< num_fields
; i
++)
2876 decode_glsl_struct_field_from_blob(blob
, &fields
[i
]);
2879 if (base_type
== GLSL_TYPE_INTERFACE
) {
2880 enum glsl_interface_packing packing
=
2881 (glsl_interface_packing
) encoded
.strct
.interface_packing_or_packed
;
2882 bool row_major
= encoded
.strct
.interface_row_major
;
2883 t
= glsl_type::get_interface_instance(fields
, num_fields
, packing
,
2886 unsigned packed
= encoded
.strct
.interface_packing_or_packed
;
2887 t
= glsl_type::get_struct_instance(fields
, num_fields
, name
, packed
);
2893 case GLSL_TYPE_VOID
:
2894 return glsl_type::void_type
;
2895 case GLSL_TYPE_ERROR
:
2897 assert(!"Cannot decode type!");
2903 glsl_type::cl_alignment() const
2905 /* vectors unlike arrays are aligned to their size */
2906 if (this->is_scalar() || this->is_vector())
2907 return this->cl_size();
2908 else if (this->is_array())
2909 return this->without_array()->cl_alignment();
2910 else if (this->is_struct()) {
2911 /* Packed Structs are 0x1 aligned despite their size. */
2916 for (unsigned i
= 0; i
< this->length
; ++i
) {
2917 struct glsl_struct_field
&field
= this->fields
.structure
[i
];
2918 res
= MAX2(res
, field
.type
->cl_alignment());
2926 glsl_type::cl_size() const
2928 if (this->is_scalar()) {
2929 return glsl_base_type_get_bit_size(this->base_type
) / 8;
2930 } else if (this->is_vector()) {
2931 unsigned vec_elemns
= this->vector_elements
== 3 ? 4 : this->vector_elements
;
2932 return vec_elemns
* glsl_base_type_get_bit_size(this->base_type
) / 8;
2933 } else if (this->is_array()) {
2934 unsigned size
= this->without_array()->cl_size();
2935 return size
* this->length
;
2936 } else if (this->is_struct()) {
2938 for (unsigned i
= 0; i
< this->length
; ++i
) {
2939 struct glsl_struct_field
&field
= this->fields
.structure
[i
];
2940 /* if a struct is packed, members don't get aligned */
2942 size
= align(size
, field
.type
->cl_alignment());
2943 size
+= field
.type
->cl_size();
2953 glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim
)
2956 case GLSL_SAMPLER_DIM_1D
:
2957 case GLSL_SAMPLER_DIM_BUF
:
2959 case GLSL_SAMPLER_DIM_2D
:
2960 case GLSL_SAMPLER_DIM_RECT
:
2961 case GLSL_SAMPLER_DIM_MS
:
2962 case GLSL_SAMPLER_DIM_EXTERNAL
:
2963 case GLSL_SAMPLER_DIM_SUBPASS
:
2964 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2966 case GLSL_SAMPLER_DIM_3D
:
2967 case GLSL_SAMPLER_DIM_CUBE
:
2970 unreachable("Unknown sampler dim");