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::record_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 glsl_type::glsl_type(GLenum gl_type
,
41 glsl_base_type base_type
, unsigned vector_elements
,
42 unsigned matrix_columns
, const char *name
,
43 unsigned explicit_stride
, bool row_major
) :
45 base_type(base_type
), sampled_type(GLSL_TYPE_VOID
),
46 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
47 interface_packing(0), interface_row_major(row_major
),
48 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
49 length(0), explicit_stride(explicit_stride
)
51 /* Values of these types must fit in the two bits of
52 * glsl_type::sampled_type.
54 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT
) & 3) == unsigned(GLSL_TYPE_UINT
));
55 STATIC_ASSERT((unsigned(GLSL_TYPE_INT
) & 3) == unsigned(GLSL_TYPE_INT
));
56 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT
) & 3) == unsigned(GLSL_TYPE_FLOAT
));
58 ASSERT_BITFIELD_SIZE(glsl_type
, base_type
, GLSL_TYPE_ERROR
);
59 ASSERT_BITFIELD_SIZE(glsl_type
, sampled_type
, GLSL_TYPE_ERROR
);
60 ASSERT_BITFIELD_SIZE(glsl_type
, sampler_dimensionality
,
61 GLSL_SAMPLER_DIM_SUBPASS_MS
);
63 this->mem_ctx
= ralloc_context(NULL
);
64 assert(this->mem_ctx
!= NULL
);
67 this->name
= ralloc_strdup(this->mem_ctx
, name
);
69 /* Neither dimension is zero or both dimensions are zero.
71 assert((vector_elements
== 0) == (matrix_columns
== 0));
72 memset(& fields
, 0, sizeof(fields
));
75 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
76 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
77 glsl_base_type type
, const char *name
) :
79 base_type(base_type
), sampled_type(type
),
80 sampler_dimensionality(dim
), sampler_shadow(shadow
),
81 sampler_array(array
), interface_packing(0),
82 interface_row_major(0),
83 length(0), explicit_stride(0)
85 this->mem_ctx
= ralloc_context(NULL
);
86 assert(this->mem_ctx
!= NULL
);
89 this->name
= ralloc_strdup(this->mem_ctx
, name
);
91 memset(& fields
, 0, sizeof(fields
));
93 matrix_columns
= vector_elements
= 1;
96 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
99 base_type(GLSL_TYPE_STRUCT
), sampled_type(GLSL_TYPE_VOID
),
100 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
101 interface_packing(0), interface_row_major(0),
102 vector_elements(0), matrix_columns(0),
103 length(num_fields
), explicit_stride(0)
107 this->mem_ctx
= ralloc_context(NULL
);
108 assert(this->mem_ctx
!= NULL
);
110 assert(name
!= NULL
);
111 this->name
= ralloc_strdup(this->mem_ctx
, name
);
112 /* Zero-fill to prevent spurious Valgrind errors when serializing NIR
113 * due to uninitialized unused bits in bit fields. */
114 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
115 glsl_struct_field
, length
);
117 for (i
= 0; i
< length
; i
++) {
118 this->fields
.structure
[i
] = fields
[i
];
119 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
124 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
125 enum glsl_interface_packing packing
,
126 bool row_major
, const char *name
) :
128 base_type(GLSL_TYPE_INTERFACE
), sampled_type(GLSL_TYPE_VOID
),
129 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
130 interface_packing((unsigned) packing
),
131 interface_row_major((unsigned) row_major
),
132 vector_elements(0), matrix_columns(0),
133 length(num_fields
), explicit_stride(0)
137 this->mem_ctx
= ralloc_context(NULL
);
138 assert(this->mem_ctx
!= NULL
);
140 assert(name
!= NULL
);
141 this->name
= ralloc_strdup(this->mem_ctx
, name
);
142 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
143 glsl_struct_field
, length
);
144 for (i
= 0; i
< length
; i
++) {
145 this->fields
.structure
[i
] = fields
[i
];
146 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
151 glsl_type::glsl_type(const glsl_type
*return_type
,
152 const glsl_function_param
*params
, unsigned num_params
) :
154 base_type(GLSL_TYPE_FUNCTION
), sampled_type(GLSL_TYPE_VOID
),
155 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
156 interface_packing(0), interface_row_major(0),
157 vector_elements(0), matrix_columns(0),
158 length(num_params
), explicit_stride(0)
162 this->mem_ctx
= ralloc_context(NULL
);
163 assert(this->mem_ctx
!= NULL
);
165 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
166 glsl_function_param
, num_params
+ 1);
168 /* We store the return type as the first parameter */
169 this->fields
.parameters
[0].type
= return_type
;
170 this->fields
.parameters
[0].in
= false;
171 this->fields
.parameters
[0].out
= true;
173 /* We store the i'th parameter in slot i+1 */
174 for (i
= 0; i
< length
; i
++) {
175 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
176 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
177 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
181 glsl_type::glsl_type(const char *subroutine_name
) :
183 base_type(GLSL_TYPE_SUBROUTINE
), sampled_type(GLSL_TYPE_VOID
),
184 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
185 interface_packing(0), interface_row_major(0),
186 vector_elements(1), matrix_columns(1),
187 length(0), explicit_stride(0)
189 this->mem_ctx
= ralloc_context(NULL
);
190 assert(this->mem_ctx
!= NULL
);
192 assert(subroutine_name
!= NULL
);
193 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
196 glsl_type::~glsl_type()
198 ralloc_free(this->mem_ctx
);
202 glsl_type::contains_sampler() const
204 if (this->is_array()) {
205 return this->fields
.array
->contains_sampler();
206 } else if (this->is_struct() || this->is_interface()) {
207 for (unsigned int i
= 0; i
< this->length
; i
++) {
208 if (this->fields
.structure
[i
].type
->contains_sampler())
213 return this->is_sampler();
218 glsl_type::contains_array() const
220 if (this->is_struct() || this->is_interface()) {
221 for (unsigned int i
= 0; i
< this->length
; i
++) {
222 if (this->fields
.structure
[i
].type
->contains_array())
227 return this->is_array();
232 glsl_type::contains_integer() const
234 if (this->is_array()) {
235 return this->fields
.array
->contains_integer();
236 } else if (this->is_struct() || this->is_interface()) {
237 for (unsigned int i
= 0; i
< this->length
; i
++) {
238 if (this->fields
.structure
[i
].type
->contains_integer())
243 return this->is_integer();
248 glsl_type::contains_double() const
250 if (this->is_array()) {
251 return this->fields
.array
->contains_double();
252 } else if (this->is_struct() || this->is_interface()) {
253 for (unsigned int i
= 0; i
< this->length
; i
++) {
254 if (this->fields
.structure
[i
].type
->contains_double())
259 return this->is_double();
264 glsl_type::contains_64bit() const
266 if (this->is_array()) {
267 return this->fields
.array
->contains_64bit();
268 } else if (this->is_struct() || this->is_interface()) {
269 for (unsigned int i
= 0; i
< this->length
; i
++) {
270 if (this->fields
.structure
[i
].type
->contains_64bit())
275 return this->is_64bit();
280 glsl_type::contains_opaque() const {
282 case GLSL_TYPE_SAMPLER
:
283 case GLSL_TYPE_IMAGE
:
284 case GLSL_TYPE_ATOMIC_UINT
:
286 case GLSL_TYPE_ARRAY
:
287 return fields
.array
->contains_opaque();
288 case GLSL_TYPE_STRUCT
:
289 case GLSL_TYPE_INTERFACE
:
290 for (unsigned int i
= 0; i
< length
; i
++) {
291 if (fields
.structure
[i
].type
->contains_opaque())
301 glsl_type::contains_subroutine() const
303 if (this->is_array()) {
304 return this->fields
.array
->contains_subroutine();
305 } else if (this->is_struct() || this->is_interface()) {
306 for (unsigned int i
= 0; i
< this->length
; i
++) {
307 if (this->fields
.structure
[i
].type
->contains_subroutine())
312 return this->is_subroutine();
317 glsl_type::sampler_index() const
319 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
321 assert(t
->is_sampler() || t
->is_image());
323 switch (t
->sampler_dimensionality
) {
324 case GLSL_SAMPLER_DIM_1D
:
325 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
326 case GLSL_SAMPLER_DIM_2D
:
327 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
328 case GLSL_SAMPLER_DIM_3D
:
329 return TEXTURE_3D_INDEX
;
330 case GLSL_SAMPLER_DIM_CUBE
:
331 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
332 case GLSL_SAMPLER_DIM_RECT
:
333 return TEXTURE_RECT_INDEX
;
334 case GLSL_SAMPLER_DIM_BUF
:
335 return TEXTURE_BUFFER_INDEX
;
336 case GLSL_SAMPLER_DIM_EXTERNAL
:
337 return TEXTURE_EXTERNAL_INDEX
;
338 case GLSL_SAMPLER_DIM_MS
:
339 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
341 assert(!"Should not get here.");
342 return TEXTURE_BUFFER_INDEX
;
347 glsl_type::contains_image() const
349 if (this->is_array()) {
350 return this->fields
.array
->contains_image();
351 } else if (this->is_struct() || this->is_interface()) {
352 for (unsigned int i
= 0; i
< this->length
; i
++) {
353 if (this->fields
.structure
[i
].type
->contains_image())
358 return this->is_image();
362 const glsl_type
*glsl_type::get_base_type() const
367 case GLSL_TYPE_UINT16
:
368 return uint16_t_type
;
369 case GLSL_TYPE_UINT8
:
373 case GLSL_TYPE_INT16
:
377 case GLSL_TYPE_FLOAT
:
379 case GLSL_TYPE_FLOAT16
:
380 return float16_t_type
;
381 case GLSL_TYPE_DOUBLE
:
385 case GLSL_TYPE_UINT64
:
386 return uint64_t_type
;
387 case GLSL_TYPE_INT64
:
395 const glsl_type
*glsl_type::get_scalar_type() const
397 const glsl_type
*type
= this;
400 while (type
->base_type
== GLSL_TYPE_ARRAY
)
401 type
= type
->fields
.array
;
403 const glsl_type
*scalar_type
= type
->get_base_type();
404 if (scalar_type
== error_type
)
411 const glsl_type
*glsl_type::get_bare_type() const
413 switch (this->base_type
) {
414 case GLSL_TYPE_UINT8
:
416 case GLSL_TYPE_UINT16
:
417 case GLSL_TYPE_INT16
:
418 case GLSL_TYPE_FLOAT16
:
421 case GLSL_TYPE_FLOAT
:
423 case GLSL_TYPE_DOUBLE
:
424 case GLSL_TYPE_UINT64
:
425 case GLSL_TYPE_INT64
:
426 return get_instance(this->base_type
, this->vector_elements
,
427 this->matrix_columns
);
429 case GLSL_TYPE_STRUCT
:
430 case GLSL_TYPE_INTERFACE
: {
431 glsl_struct_field
*bare_fields
= new glsl_struct_field
[this->length
];
432 for (unsigned i
= 0; i
< this->length
; i
++) {
433 bare_fields
[i
].type
= this->fields
.structure
[i
].type
->get_bare_type();
434 bare_fields
[i
].name
= this->fields
.structure
[i
].name
;
436 const glsl_type
*bare_type
=
437 get_record_instance(bare_fields
, this->length
, this->name
);
438 delete[] bare_fields
;
442 case GLSL_TYPE_ARRAY
:
443 return get_array_instance(this->fields
.array
->get_bare_type(),
446 case GLSL_TYPE_SAMPLER
:
447 case GLSL_TYPE_IMAGE
:
448 case GLSL_TYPE_ATOMIC_UINT
:
450 case GLSL_TYPE_SUBROUTINE
:
451 case GLSL_TYPE_FUNCTION
:
452 case GLSL_TYPE_ERROR
:
456 unreachable("Invalid base type");
461 hash_free_type_function(struct hash_entry
*entry
)
463 glsl_type
*type
= (glsl_type
*) entry
->data
;
465 if (type
->is_array())
466 free((void*)entry
->key
);
472 _mesa_glsl_release_types(void)
474 /* Should only be called during atexit (either when unloading shared
475 * object, or if process terminates), so no mutex-locking should be
478 if (glsl_type::explicit_matrix_types
!= NULL
) {
479 _mesa_hash_table_destroy(glsl_type::explicit_matrix_types
,
480 hash_free_type_function
);
481 glsl_type::explicit_matrix_types
= NULL
;
484 if (glsl_type::array_types
!= NULL
) {
485 _mesa_hash_table_destroy(glsl_type::array_types
, hash_free_type_function
);
486 glsl_type::array_types
= NULL
;
489 if (glsl_type::record_types
!= NULL
) {
490 _mesa_hash_table_destroy(glsl_type::record_types
, hash_free_type_function
);
491 glsl_type::record_types
= NULL
;
494 if (glsl_type::interface_types
!= NULL
) {
495 _mesa_hash_table_destroy(glsl_type::interface_types
, hash_free_type_function
);
496 glsl_type::interface_types
= NULL
;
499 if (glsl_type::function_types
!= NULL
) {
500 _mesa_hash_table_destroy(glsl_type::function_types
, hash_free_type_function
);
501 glsl_type::function_types
= NULL
;
504 if (glsl_type::subroutine_types
!= NULL
) {
505 _mesa_hash_table_destroy(glsl_type::subroutine_types
, hash_free_type_function
);
506 glsl_type::subroutine_types
= NULL
;
511 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
,
512 unsigned explicit_stride
) :
513 base_type(GLSL_TYPE_ARRAY
), sampled_type(GLSL_TYPE_VOID
),
514 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
515 interface_packing(0), interface_row_major(0),
516 vector_elements(0), matrix_columns(0),
517 length(length
), name(NULL
), explicit_stride(explicit_stride
)
519 this->fields
.array
= array
;
520 /* Inherit the gl type of the base. The GL type is used for
521 * uniform/statevar handling in Mesa and the arrayness of the type
522 * is represented by the size rather than the type.
524 this->gl_type
= array
->gl_type
;
526 /* Allow a maximum of 10 characters for the array size. This is enough
527 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
530 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
532 this->mem_ctx
= ralloc_context(NULL
);
533 assert(this->mem_ctx
!= NULL
);
535 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
538 util_snprintf(n
, name_length
, "%s[]", array
->name
);
540 /* insert outermost dimensions in the correct spot
541 * otherwise the dimension order will be backwards
543 const char *pos
= strchr(array
->name
, '[');
545 int idx
= pos
- array
->name
;
546 util_snprintf(n
, idx
+1, "%s", array
->name
);
547 util_snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
548 length
, array
->name
+ idx
);
550 util_snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
558 glsl_type::vec(unsigned components
, const glsl_type
*const ts
[])
560 unsigned n
= components
;
564 else if (components
== 16)
573 #define VECN(components, sname, vname) \
575 glsl_type:: vname (unsigned components) \
577 static const glsl_type *const ts[] = { \
578 sname ## _type, vname ## 2_type, \
579 vname ## 3_type, vname ## 4_type, \
580 vname ## 8_type, vname ## 16_type, \
582 return glsl_type::vec(components, ts); \
585 VECN(components
, float, vec
)
586 VECN(components
, float16_t
, f16vec
)
587 VECN(components
, double, dvec
)
588 VECN(components
, int, ivec
)
589 VECN(components
, uint
, uvec
)
590 VECN(components
, bool, bvec
)
591 VECN(components
, int64_t, i64vec
)
592 VECN(components
, uint64_t, u64vec
)
593 VECN(components
, int16_t, i16vec
)
594 VECN(components
, uint16_t, u16vec
)
595 VECN(components
, int8_t, i8vec
)
596 VECN(components
, uint8_t, u8vec
)
599 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
,
600 unsigned explicit_stride
, bool row_major
)
602 if (base_type
== GLSL_TYPE_VOID
) {
603 assert(explicit_stride
== 0 && !row_major
);
607 /* Matrix and vector types with explicit strides have to be looked up in a
608 * table so they're handled separately.
610 if (explicit_stride
> 0) {
611 const glsl_type
*bare_type
= get_instance(base_type
, rows
, columns
);
613 assert(columns
> 1 || !row_major
);
616 util_snprintf(name
, sizeof(name
), "%sx%uB%s", bare_type
->name
,
617 explicit_stride
, row_major
? "RM" : "");
619 mtx_lock(&glsl_type::hash_mutex
);
621 if (explicit_matrix_types
== NULL
) {
622 explicit_matrix_types
=
623 _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
624 _mesa_key_string_equal
);
627 const struct hash_entry
*entry
=
628 _mesa_hash_table_search(explicit_matrix_types
, name
);
630 const glsl_type
*t
= new glsl_type(bare_type
->gl_type
,
631 (glsl_base_type
)base_type
,
633 explicit_stride
, row_major
);
635 entry
= _mesa_hash_table_insert(explicit_matrix_types
,
639 assert(((glsl_type
*) entry
->data
)->base_type
== base_type
);
640 assert(((glsl_type
*) entry
->data
)->vector_elements
== rows
);
641 assert(((glsl_type
*) entry
->data
)->matrix_columns
== columns
);
642 assert(((glsl_type
*) entry
->data
)->explicit_stride
== explicit_stride
);
644 mtx_unlock(&glsl_type::hash_mutex
);
646 return (const glsl_type
*) entry
->data
;
651 /* Treat GLSL vectors as Nx1 matrices.
659 case GLSL_TYPE_FLOAT
:
661 case GLSL_TYPE_FLOAT16
:
663 case GLSL_TYPE_DOUBLE
:
667 case GLSL_TYPE_UINT64
:
669 case GLSL_TYPE_INT64
:
671 case GLSL_TYPE_UINT16
:
673 case GLSL_TYPE_INT16
:
675 case GLSL_TYPE_UINT8
:
683 if ((base_type
!= GLSL_TYPE_FLOAT
&&
684 base_type
!= GLSL_TYPE_DOUBLE
&&
685 base_type
!= GLSL_TYPE_FLOAT16
) || (rows
== 1))
688 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
689 * combinations are valid:
697 #define IDX(c,r) (((c-1)*3) + (r-1))
700 case GLSL_TYPE_DOUBLE
: {
701 switch (IDX(columns
, rows
)) {
702 case IDX(2,2): return dmat2_type
;
703 case IDX(2,3): return dmat2x3_type
;
704 case IDX(2,4): return dmat2x4_type
;
705 case IDX(3,2): return dmat3x2_type
;
706 case IDX(3,3): return dmat3_type
;
707 case IDX(3,4): return dmat3x4_type
;
708 case IDX(4,2): return dmat4x2_type
;
709 case IDX(4,3): return dmat4x3_type
;
710 case IDX(4,4): return dmat4_type
;
711 default: return error_type
;
714 case GLSL_TYPE_FLOAT
: {
715 switch (IDX(columns
, rows
)) {
716 case IDX(2,2): return mat2_type
;
717 case IDX(2,3): return mat2x3_type
;
718 case IDX(2,4): return mat2x4_type
;
719 case IDX(3,2): return mat3x2_type
;
720 case IDX(3,3): return mat3_type
;
721 case IDX(3,4): return mat3x4_type
;
722 case IDX(4,2): return mat4x2_type
;
723 case IDX(4,3): return mat4x3_type
;
724 case IDX(4,4): return mat4_type
;
725 default: return error_type
;
728 case GLSL_TYPE_FLOAT16
: {
729 switch (IDX(columns
, rows
)) {
730 case IDX(2,2): return f16mat2_type
;
731 case IDX(2,3): return f16mat2x3_type
;
732 case IDX(2,4): return f16mat2x4_type
;
733 case IDX(3,2): return f16mat3x2_type
;
734 case IDX(3,3): return f16mat3_type
;
735 case IDX(3,4): return f16mat3x4_type
;
736 case IDX(4,2): return f16mat4x2_type
;
737 case IDX(4,3): return f16mat4x3_type
;
738 case IDX(4,4): return f16mat4_type
;
739 default: return error_type
;
742 default: return error_type
;
746 assert(!"Should not get here.");
751 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
757 case GLSL_TYPE_FLOAT
:
759 case GLSL_SAMPLER_DIM_1D
:
761 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
763 return (array
? sampler1DArray_type
: sampler1D_type
);
764 case GLSL_SAMPLER_DIM_2D
:
766 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
768 return (array
? sampler2DArray_type
: sampler2D_type
);
769 case GLSL_SAMPLER_DIM_3D
:
773 return sampler3D_type
;
774 case GLSL_SAMPLER_DIM_CUBE
:
776 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
778 return (array
? samplerCubeArray_type
: samplerCube_type
);
779 case GLSL_SAMPLER_DIM_RECT
:
783 return sampler2DRectShadow_type
;
785 return sampler2DRect_type
;
786 case GLSL_SAMPLER_DIM_BUF
:
790 return samplerBuffer_type
;
791 case GLSL_SAMPLER_DIM_MS
:
794 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
795 case GLSL_SAMPLER_DIM_EXTERNAL
:
799 return samplerExternalOES_type
;
800 case GLSL_SAMPLER_DIM_SUBPASS
:
801 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
808 case GLSL_SAMPLER_DIM_1D
:
809 return (array
? isampler1DArray_type
: isampler1D_type
);
810 case GLSL_SAMPLER_DIM_2D
:
811 return (array
? isampler2DArray_type
: isampler2D_type
);
812 case GLSL_SAMPLER_DIM_3D
:
815 return isampler3D_type
;
816 case GLSL_SAMPLER_DIM_CUBE
:
817 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
818 case GLSL_SAMPLER_DIM_RECT
:
821 return isampler2DRect_type
;
822 case GLSL_SAMPLER_DIM_BUF
:
825 return isamplerBuffer_type
;
826 case GLSL_SAMPLER_DIM_MS
:
827 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
828 case GLSL_SAMPLER_DIM_EXTERNAL
:
830 case GLSL_SAMPLER_DIM_SUBPASS
:
831 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
838 case GLSL_SAMPLER_DIM_1D
:
839 return (array
? usampler1DArray_type
: usampler1D_type
);
840 case GLSL_SAMPLER_DIM_2D
:
841 return (array
? usampler2DArray_type
: usampler2D_type
);
842 case GLSL_SAMPLER_DIM_3D
:
845 return usampler3D_type
;
846 case GLSL_SAMPLER_DIM_CUBE
:
847 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
848 case GLSL_SAMPLER_DIM_RECT
:
851 return usampler2DRect_type
;
852 case GLSL_SAMPLER_DIM_BUF
:
855 return usamplerBuffer_type
;
856 case GLSL_SAMPLER_DIM_MS
:
857 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
858 case GLSL_SAMPLER_DIM_EXTERNAL
:
860 case GLSL_SAMPLER_DIM_SUBPASS
:
861 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
868 unreachable("switch statement above should be complete");
872 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
873 bool array
, glsl_base_type type
)
876 case GLSL_TYPE_FLOAT
:
878 case GLSL_SAMPLER_DIM_1D
:
879 return (array
? image1DArray_type
: image1D_type
);
880 case GLSL_SAMPLER_DIM_2D
:
881 return (array
? image2DArray_type
: image2D_type
);
882 case GLSL_SAMPLER_DIM_3D
:
884 case GLSL_SAMPLER_DIM_CUBE
:
885 return (array
? imageCubeArray_type
: imageCube_type
);
886 case GLSL_SAMPLER_DIM_RECT
:
890 return image2DRect_type
;
891 case GLSL_SAMPLER_DIM_BUF
:
895 return imageBuffer_type
;
896 case GLSL_SAMPLER_DIM_MS
:
897 return (array
? image2DMSArray_type
: image2DMS_type
);
898 case GLSL_SAMPLER_DIM_SUBPASS
:
899 return subpassInput_type
;
900 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
901 return subpassInputMS_type
;
902 case GLSL_SAMPLER_DIM_EXTERNAL
:
907 case GLSL_SAMPLER_DIM_1D
:
908 return (array
? iimage1DArray_type
: iimage1D_type
);
909 case GLSL_SAMPLER_DIM_2D
:
910 return (array
? iimage2DArray_type
: iimage2D_type
);
911 case GLSL_SAMPLER_DIM_3D
:
914 return iimage3D_type
;
915 case GLSL_SAMPLER_DIM_CUBE
:
916 return (array
? iimageCubeArray_type
: iimageCube_type
);
917 case GLSL_SAMPLER_DIM_RECT
:
920 return iimage2DRect_type
;
921 case GLSL_SAMPLER_DIM_BUF
:
924 return iimageBuffer_type
;
925 case GLSL_SAMPLER_DIM_MS
:
926 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
927 case GLSL_SAMPLER_DIM_SUBPASS
:
928 return isubpassInput_type
;
929 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
930 return isubpassInputMS_type
;
931 case GLSL_SAMPLER_DIM_EXTERNAL
:
936 case GLSL_SAMPLER_DIM_1D
:
937 return (array
? uimage1DArray_type
: uimage1D_type
);
938 case GLSL_SAMPLER_DIM_2D
:
939 return (array
? uimage2DArray_type
: uimage2D_type
);
940 case GLSL_SAMPLER_DIM_3D
:
943 return uimage3D_type
;
944 case GLSL_SAMPLER_DIM_CUBE
:
945 return (array
? uimageCubeArray_type
: uimageCube_type
);
946 case GLSL_SAMPLER_DIM_RECT
:
949 return uimage2DRect_type
;
950 case GLSL_SAMPLER_DIM_BUF
:
953 return uimageBuffer_type
;
954 case GLSL_SAMPLER_DIM_MS
:
955 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
956 case GLSL_SAMPLER_DIM_SUBPASS
:
957 return usubpassInput_type
;
958 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
959 return usubpassInputMS_type
;
960 case GLSL_SAMPLER_DIM_EXTERNAL
:
967 unreachable("switch statement above should be complete");
971 glsl_type::get_array_instance(const glsl_type
*base
,
973 unsigned explicit_stride
)
975 /* Generate a name using the base type pointer in the key. This is
976 * done because the name of the base type may not be unique across
977 * shaders. For example, two shaders may have different record types
981 util_snprintf(key
, sizeof(key
), "%p[%u]x%uB", (void *) base
, array_size
,
984 mtx_lock(&glsl_type::hash_mutex
);
986 if (array_types
== NULL
) {
987 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
988 _mesa_key_string_equal
);
991 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
993 const glsl_type
*t
= new glsl_type(base
, array_size
, explicit_stride
);
995 entry
= _mesa_hash_table_insert(array_types
,
1000 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
1001 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
1002 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
1004 mtx_unlock(&glsl_type::hash_mutex
);
1006 return (glsl_type
*) entry
->data
;
1011 glsl_type::record_compare(const glsl_type
*b
, bool match_locations
) const
1013 if (this->length
!= b
->length
)
1016 if (this->interface_packing
!= b
->interface_packing
)
1019 if (this->interface_row_major
!= b
->interface_row_major
)
1022 /* From the GLSL 4.20 specification (Sec 4.2):
1024 * "Structures must have the same name, sequence of type names, and
1025 * type definitions, and field names to be considered the same type."
1027 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
1029 if (strcmp(this->name
, b
->name
) != 0)
1032 for (unsigned i
= 0; i
< this->length
; i
++) {
1033 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
1035 if (strcmp(this->fields
.structure
[i
].name
,
1036 b
->fields
.structure
[i
].name
) != 0)
1038 if (this->fields
.structure
[i
].matrix_layout
1039 != b
->fields
.structure
[i
].matrix_layout
)
1041 if (match_locations
&& this->fields
.structure
[i
].location
1042 != b
->fields
.structure
[i
].location
)
1044 if (this->fields
.structure
[i
].offset
1045 != b
->fields
.structure
[i
].offset
)
1047 if (this->fields
.structure
[i
].interpolation
1048 != b
->fields
.structure
[i
].interpolation
)
1050 if (this->fields
.structure
[i
].centroid
1051 != b
->fields
.structure
[i
].centroid
)
1053 if (this->fields
.structure
[i
].sample
1054 != b
->fields
.structure
[i
].sample
)
1056 if (this->fields
.structure
[i
].patch
1057 != b
->fields
.structure
[i
].patch
)
1059 if (this->fields
.structure
[i
].memory_read_only
1060 != b
->fields
.structure
[i
].memory_read_only
)
1062 if (this->fields
.structure
[i
].memory_write_only
1063 != b
->fields
.structure
[i
].memory_write_only
)
1065 if (this->fields
.structure
[i
].memory_coherent
1066 != b
->fields
.structure
[i
].memory_coherent
)
1068 if (this->fields
.structure
[i
].memory_volatile
1069 != b
->fields
.structure
[i
].memory_volatile
)
1071 if (this->fields
.structure
[i
].memory_restrict
1072 != b
->fields
.structure
[i
].memory_restrict
)
1074 if (this->fields
.structure
[i
].image_format
1075 != b
->fields
.structure
[i
].image_format
)
1077 if (this->fields
.structure
[i
].precision
1078 != b
->fields
.structure
[i
].precision
)
1080 if (this->fields
.structure
[i
].explicit_xfb_buffer
1081 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
1083 if (this->fields
.structure
[i
].xfb_buffer
1084 != b
->fields
.structure
[i
].xfb_buffer
)
1086 if (this->fields
.structure
[i
].xfb_stride
1087 != b
->fields
.structure
[i
].xfb_stride
)
1096 glsl_type::record_key_compare(const void *a
, const void *b
)
1098 const glsl_type
*const key1
= (glsl_type
*) a
;
1099 const glsl_type
*const key2
= (glsl_type
*) b
;
1101 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
1106 * Generate an integer hash value for a glsl_type structure type.
1109 glsl_type::record_key_hash(const void *a
)
1111 const glsl_type
*const key
= (glsl_type
*) a
;
1112 uintptr_t hash
= key
->length
;
1115 for (unsigned i
= 0; i
< key
->length
; i
++) {
1116 /* casting pointer to uintptr_t */
1117 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
1120 if (sizeof(hash
) == 8)
1121 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
1130 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
1131 unsigned num_fields
,
1134 const glsl_type
key(fields
, num_fields
, name
);
1136 mtx_lock(&glsl_type::hash_mutex
);
1138 if (record_types
== NULL
) {
1139 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1140 record_key_compare
);
1143 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
1145 if (entry
== NULL
) {
1146 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
1148 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
1151 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1152 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1153 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1155 mtx_unlock(&glsl_type::hash_mutex
);
1157 return (glsl_type
*) entry
->data
;
1162 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1163 unsigned num_fields
,
1164 enum glsl_interface_packing packing
,
1166 const char *block_name
)
1168 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1170 mtx_lock(&glsl_type::hash_mutex
);
1172 if (interface_types
== NULL
) {
1173 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1174 record_key_compare
);
1177 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1179 if (entry
== NULL
) {
1180 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1181 packing
, row_major
, block_name
);
1183 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1186 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1187 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1188 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1190 mtx_unlock(&glsl_type::hash_mutex
);
1192 return (glsl_type
*) entry
->data
;
1196 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1198 const glsl_type
key(subroutine_name
);
1200 mtx_lock(&glsl_type::hash_mutex
);
1202 if (subroutine_types
== NULL
) {
1203 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1204 record_key_compare
);
1207 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1209 if (entry
== NULL
) {
1210 const glsl_type
*t
= new glsl_type(subroutine_name
);
1212 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1215 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1216 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1218 mtx_unlock(&glsl_type::hash_mutex
);
1220 return (glsl_type
*) entry
->data
;
1225 function_key_compare(const void *a
, const void *b
)
1227 const glsl_type
*const key1
= (glsl_type
*) a
;
1228 const glsl_type
*const key2
= (glsl_type
*) b
;
1230 if (key1
->length
!= key2
->length
)
1233 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1234 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1239 function_key_hash(const void *a
)
1241 const glsl_type
*const key
= (glsl_type
*) a
;
1242 return _mesa_hash_data(key
->fields
.parameters
,
1243 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1247 glsl_type::get_function_instance(const glsl_type
*return_type
,
1248 const glsl_function_param
*params
,
1249 unsigned num_params
)
1251 const glsl_type
key(return_type
, params
, num_params
);
1253 mtx_lock(&glsl_type::hash_mutex
);
1255 if (function_types
== NULL
) {
1256 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1257 function_key_compare
);
1260 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1261 if (entry
== NULL
) {
1262 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1264 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1267 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1269 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1270 assert(t
->length
== num_params
);
1272 mtx_unlock(&glsl_type::hash_mutex
);
1279 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1281 if (type_a
== type_b
) {
1283 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1284 /* Matrix multiply. The columns of A must match the rows of B. Given
1285 * the other previously tested constraints, this means the vector type
1286 * of a row from A must be the same as the vector type of a column from
1289 if (type_a
->row_type() == type_b
->column_type()) {
1290 /* The resulting matrix has the number of columns of matrix B and
1291 * the number of rows of matrix A. We get the row count of A by
1292 * looking at the size of a vector that makes up a column. The
1293 * transpose (size of a row) is done for B.
1295 const glsl_type
*const type
=
1296 get_instance(type_a
->base_type
,
1297 type_a
->column_type()->vector_elements
,
1298 type_b
->row_type()->vector_elements
);
1299 assert(type
!= error_type
);
1303 } else if (type_a
->is_matrix()) {
1304 /* A is a matrix and B is a column vector. Columns of A must match
1305 * rows of B. Given the other previously tested constraints, this
1306 * means the vector type of a row from A must be the same as the
1307 * vector the type of B.
1309 if (type_a
->row_type() == type_b
) {
1310 /* The resulting vector has a number of elements equal to
1311 * the number of rows of matrix A. */
1312 const glsl_type
*const type
=
1313 get_instance(type_a
->base_type
,
1314 type_a
->column_type()->vector_elements
,
1316 assert(type
!= error_type
);
1321 assert(type_b
->is_matrix());
1323 /* A is a row vector and B is a matrix. Columns of A must match rows
1324 * of B. Given the other previously tested constraints, this means
1325 * the type of A must be the same as the vector type of a column from
1328 if (type_a
== type_b
->column_type()) {
1329 /* The resulting vector has a number of elements equal to
1330 * the number of columns of matrix B. */
1331 const glsl_type
*const type
=
1332 get_instance(type_a
->base_type
,
1333 type_b
->row_type()->vector_elements
,
1335 assert(type
!= error_type
);
1346 glsl_type::field_type(const char *name
) const
1348 if (this->base_type
!= GLSL_TYPE_STRUCT
1349 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1352 for (unsigned i
= 0; i
< this->length
; i
++) {
1353 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1354 return this->fields
.structure
[i
].type
;
1362 glsl_type::field_index(const char *name
) const
1364 if (this->base_type
!= GLSL_TYPE_STRUCT
1365 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1368 for (unsigned i
= 0; i
< this->length
; i
++) {
1369 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1378 glsl_type::component_slots() const
1380 switch (this->base_type
) {
1381 case GLSL_TYPE_UINT
:
1383 case GLSL_TYPE_UINT8
:
1384 case GLSL_TYPE_INT8
:
1385 case GLSL_TYPE_UINT16
:
1386 case GLSL_TYPE_INT16
:
1387 case GLSL_TYPE_FLOAT
:
1388 case GLSL_TYPE_FLOAT16
:
1389 case GLSL_TYPE_BOOL
:
1390 return this->components();
1392 case GLSL_TYPE_DOUBLE
:
1393 case GLSL_TYPE_UINT64
:
1394 case GLSL_TYPE_INT64
:
1395 return 2 * this->components();
1397 case GLSL_TYPE_STRUCT
:
1398 case GLSL_TYPE_INTERFACE
: {
1401 for (unsigned i
= 0; i
< this->length
; i
++)
1402 size
+= this->fields
.structure
[i
].type
->component_slots();
1407 case GLSL_TYPE_ARRAY
:
1408 return this->length
* this->fields
.array
->component_slots();
1410 case GLSL_TYPE_SAMPLER
:
1411 case GLSL_TYPE_IMAGE
:
1414 case GLSL_TYPE_SUBROUTINE
:
1417 case GLSL_TYPE_FUNCTION
:
1418 case GLSL_TYPE_ATOMIC_UINT
:
1419 case GLSL_TYPE_VOID
:
1420 case GLSL_TYPE_ERROR
:
1428 glsl_type::record_location_offset(unsigned length
) const
1430 unsigned offset
= 0;
1431 const glsl_type
*t
= this->without_array();
1432 if (t
->is_struct()) {
1433 assert(length
<= t
->length
);
1435 for (unsigned i
= 0; i
< length
; i
++) {
1436 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1437 const glsl_type
*wa
= st
->without_array();
1438 if (wa
->is_struct()) {
1439 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1440 offset
+= st
->is_array() ?
1441 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1442 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1443 unsigned outer_array_size
= st
->length
;
1444 const glsl_type
*base_type
= st
->fields
.array
;
1446 /* For arrays of arrays the outer arrays take up a uniform
1447 * slot for each element. The innermost array elements share a
1448 * single slot so we ignore the innermost array when calculating
1451 while (base_type
->fields
.array
->is_array()) {
1452 outer_array_size
= outer_array_size
* base_type
->length
;
1453 base_type
= base_type
->fields
.array
;
1455 offset
+= outer_array_size
;
1457 /* We dont worry about arrays here because unless the array
1458 * contains a structure or another array it only takes up a single
1469 glsl_type::uniform_locations() const
1473 switch (this->base_type
) {
1474 case GLSL_TYPE_UINT
:
1476 case GLSL_TYPE_FLOAT
:
1477 case GLSL_TYPE_FLOAT16
:
1478 case GLSL_TYPE_DOUBLE
:
1479 case GLSL_TYPE_UINT16
:
1480 case GLSL_TYPE_UINT8
:
1481 case GLSL_TYPE_INT16
:
1482 case GLSL_TYPE_INT8
:
1483 case GLSL_TYPE_UINT64
:
1484 case GLSL_TYPE_INT64
:
1485 case GLSL_TYPE_BOOL
:
1486 case GLSL_TYPE_SAMPLER
:
1487 case GLSL_TYPE_IMAGE
:
1488 case GLSL_TYPE_SUBROUTINE
:
1491 case GLSL_TYPE_STRUCT
:
1492 case GLSL_TYPE_INTERFACE
:
1493 for (unsigned i
= 0; i
< this->length
; i
++)
1494 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1496 case GLSL_TYPE_ARRAY
:
1497 return this->length
* this->fields
.array
->uniform_locations();
1504 glsl_type::varying_count() const
1508 switch (this->base_type
) {
1509 case GLSL_TYPE_UINT
:
1511 case GLSL_TYPE_FLOAT
:
1512 case GLSL_TYPE_FLOAT16
:
1513 case GLSL_TYPE_DOUBLE
:
1514 case GLSL_TYPE_BOOL
:
1515 case GLSL_TYPE_UINT16
:
1516 case GLSL_TYPE_UINT8
:
1517 case GLSL_TYPE_INT16
:
1518 case GLSL_TYPE_INT8
:
1519 case GLSL_TYPE_UINT64
:
1520 case GLSL_TYPE_INT64
:
1523 case GLSL_TYPE_STRUCT
:
1524 case GLSL_TYPE_INTERFACE
:
1525 for (unsigned i
= 0; i
< this->length
; i
++)
1526 size
+= this->fields
.structure
[i
].type
->varying_count();
1528 case GLSL_TYPE_ARRAY
:
1529 /* Don't count innermost array elements */
1530 if (this->without_array()->is_struct() ||
1531 this->without_array()->is_interface() ||
1532 this->fields
.array
->is_array())
1533 return this->length
* this->fields
.array
->varying_count();
1535 return this->fields
.array
->varying_count();
1537 assert(!"unsupported varying type");
1543 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1544 _mesa_glsl_parse_state
*state
) const
1546 if (this == desired
)
1549 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1550 * state, we're doing intra-stage function linking where these checks have
1551 * already been done.
1553 if (state
&& !state
->has_implicit_conversions())
1556 /* There is no conversion among matrix types. */
1557 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1560 /* Vector size must match. */
1561 if (this->vector_elements
!= desired
->vector_elements
)
1564 /* int and uint can be converted to float. */
1565 if (desired
->is_float() && this->is_integer())
1568 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1569 * can be converted to uint. Note that state may be NULL here, when
1570 * resolving function calls in the linker. By this time, all the
1571 * state-dependent checks have already happened though, so allow anything
1572 * that's allowed in any shader version.
1574 if ((!state
|| state
->has_implicit_uint_to_int_conversion()) &&
1575 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1578 /* No implicit conversions from double. */
1579 if ((!state
|| state
->has_double()) && this->is_double())
1582 /* Conversions from different types to double. */
1583 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1584 if (this->is_float())
1586 if (this->is_integer())
1594 glsl_type::std140_base_alignment(bool row_major
) const
1596 unsigned N
= is_64bit() ? 8 : 4;
1598 /* (1) If the member is a scalar consuming <N> basic machine units, the
1599 * base alignment is <N>.
1601 * (2) If the member is a two- or four-component vector with components
1602 * consuming <N> basic machine units, the base alignment is 2<N> or
1603 * 4<N>, respectively.
1605 * (3) If the member is a three-component vector with components consuming
1606 * <N> basic machine units, the base alignment is 4<N>.
1608 if (this->is_scalar() || this->is_vector()) {
1609 switch (this->vector_elements
) {
1620 /* (4) If the member is an array of scalars or vectors, the base alignment
1621 * and array stride are set to match the base alignment of a single
1622 * array element, according to rules (1), (2), and (3), and rounded up
1623 * to the base alignment of a vec4. The array may have padding at the
1624 * end; the base offset of the member following the array is rounded up
1625 * to the next multiple of the base alignment.
1627 * (6) If the member is an array of <S> column-major matrices with <C>
1628 * columns and <R> rows, the matrix is stored identically to a row of
1629 * <S>*<C> column vectors with <R> components each, according to rule
1632 * (8) If the member is an array of <S> row-major matrices with <C> columns
1633 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1634 * row vectors with <C> components each, according to rule (4).
1636 * (10) If the member is an array of <S> structures, the <S> elements of
1637 * the array are laid out in order, according to rule (9).
1639 if (this->is_array()) {
1640 if (this->fields
.array
->is_scalar() ||
1641 this->fields
.array
->is_vector() ||
1642 this->fields
.array
->is_matrix()) {
1643 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1645 assert(this->fields
.array
->is_struct() ||
1646 this->fields
.array
->is_array());
1647 return this->fields
.array
->std140_base_alignment(row_major
);
1651 /* (5) If the member is a column-major matrix with <C> columns and
1652 * <R> rows, the matrix is stored identically to an array of
1653 * <C> column vectors with <R> components each, according to
1656 * (7) If the member is a row-major matrix with <C> columns and <R>
1657 * rows, the matrix is stored identically to an array of <R>
1658 * row vectors with <C> components each, according to rule (4).
1660 if (this->is_matrix()) {
1661 const struct glsl_type
*vec_type
, *array_type
;
1662 int c
= this->matrix_columns
;
1663 int r
= this->vector_elements
;
1666 vec_type
= get_instance(base_type
, c
, 1);
1667 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1669 vec_type
= get_instance(base_type
, r
, 1);
1670 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1673 return array_type
->std140_base_alignment(false);
1676 /* (9) If the member is a structure, the base alignment of the
1677 * structure is <N>, where <N> is the largest base alignment
1678 * value of any of its members, and rounded up to the base
1679 * alignment of a vec4. The individual members of this
1680 * sub-structure are then assigned offsets by applying this set
1681 * of rules recursively, where the base offset of the first
1682 * member of the sub-structure is equal to the aligned offset
1683 * of the structure. The structure may have padding at the end;
1684 * the base offset of the member following the sub-structure is
1685 * rounded up to the next multiple of the base alignment of the
1688 if (this->is_struct()) {
1689 unsigned base_alignment
= 16;
1690 for (unsigned i
= 0; i
< this->length
; i
++) {
1691 bool field_row_major
= row_major
;
1692 const enum glsl_matrix_layout matrix_layout
=
1693 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1694 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1695 field_row_major
= true;
1696 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1697 field_row_major
= false;
1700 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1701 base_alignment
= MAX2(base_alignment
,
1702 field_type
->std140_base_alignment(field_row_major
));
1704 return base_alignment
;
1707 assert(!"not reached");
1712 glsl_type::std140_size(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 assert(this->explicit_stride
== 0);
1728 return this->vector_elements
* N
;
1731 /* (5) If the member is a column-major matrix with <C> columns and
1732 * <R> rows, the matrix is stored identically to an array of
1733 * <C> column vectors with <R> components each, according to
1736 * (6) If the member is an array of <S> column-major matrices with <C>
1737 * columns and <R> rows, the matrix is stored identically to a row of
1738 * <S>*<C> column vectors with <R> components each, according to rule
1741 * (7) If the member is a row-major matrix with <C> columns and <R>
1742 * rows, the matrix is stored identically to an array of <R>
1743 * row vectors with <C> components each, according to rule (4).
1745 * (8) If the member is an array of <S> row-major matrices with <C> columns
1746 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1747 * row vectors with <C> components each, according to rule (4).
1749 if (this->without_array()->is_matrix()) {
1750 const struct glsl_type
*element_type
;
1751 const struct glsl_type
*vec_type
;
1752 unsigned int array_len
;
1754 if (this->is_array()) {
1755 element_type
= this->without_array();
1756 array_len
= this->arrays_of_arrays_size();
1758 element_type
= this;
1762 assert(element_type
->explicit_stride
== 0);
1765 vec_type
= get_instance(element_type
->base_type
,
1766 element_type
->matrix_columns
, 1);
1768 array_len
*= element_type
->vector_elements
;
1770 vec_type
= get_instance(element_type
->base_type
,
1771 element_type
->vector_elements
, 1);
1772 array_len
*= element_type
->matrix_columns
;
1774 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1777 return array_type
->std140_size(false);
1780 /* (4) If the member is an array of scalars or vectors, the base alignment
1781 * and array stride are set to match the base alignment of a single
1782 * array element, according to rules (1), (2), and (3), and rounded up
1783 * to the base alignment of a vec4. The array may have padding at the
1784 * end; the base offset of the member following the array is rounded up
1785 * to the next multiple of the base alignment.
1787 * (10) If the member is an array of <S> structures, the <S> elements of
1788 * the array are laid out in order, according to rule (9).
1790 if (this->is_array()) {
1791 assert(this->explicit_stride
== 0);
1792 if (this->without_array()->is_struct()) {
1793 return this->arrays_of_arrays_size() *
1794 this->without_array()->std140_size(row_major
);
1796 unsigned element_base_align
=
1797 this->without_array()->std140_base_alignment(row_major
);
1798 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1802 /* (9) If the member is a structure, the base alignment of the
1803 * structure is <N>, where <N> is the largest base alignment
1804 * value of any of its members, and rounded up to the base
1805 * alignment of a vec4. The individual members of this
1806 * sub-structure are then assigned offsets by applying this set
1807 * of rules recursively, where the base offset of the first
1808 * member of the sub-structure is equal to the aligned offset
1809 * of the structure. The structure may have padding at the end;
1810 * the base offset of the member following the sub-structure is
1811 * rounded up to the next multiple of the base alignment of the
1814 if (this->is_struct() || this->is_interface()) {
1816 unsigned max_align
= 0;
1818 for (unsigned i
= 0; i
< this->length
; i
++) {
1819 bool field_row_major
= row_major
;
1820 const enum glsl_matrix_layout matrix_layout
=
1821 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1822 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1823 field_row_major
= true;
1824 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1825 field_row_major
= false;
1828 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1829 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1831 /* Ignore unsized arrays when calculating size */
1832 if (field_type
->is_unsized_array())
1835 size
= glsl_align(size
, align
);
1836 size
+= field_type
->std140_size(field_row_major
);
1838 max_align
= MAX2(align
, max_align
);
1840 if (field_type
->is_struct() && (i
+ 1 < this->length
))
1841 size
= glsl_align(size
, 16);
1843 size
= glsl_align(size
, MAX2(max_align
, 16));
1847 assert(!"not reached");
1852 glsl_type::std430_base_alignment(bool row_major
) const
1855 unsigned N
= is_64bit() ? 8 : 4;
1857 /* (1) If the member is a scalar consuming <N> basic machine units, the
1858 * base alignment is <N>.
1860 * (2) If the member is a two- or four-component vector with components
1861 * consuming <N> basic machine units, the base alignment is 2<N> or
1862 * 4<N>, respectively.
1864 * (3) If the member is a three-component vector with components consuming
1865 * <N> basic machine units, the base alignment is 4<N>.
1867 if (this->is_scalar() || this->is_vector()) {
1868 switch (this->vector_elements
) {
1879 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1881 * "When using the std430 storage layout, shader storage blocks will be
1882 * laid out in buffer storage identically to uniform and shader storage
1883 * blocks using the std140 layout, except that the base alignment and
1884 * stride of arrays of scalars and vectors in rule 4 and of structures
1885 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1888 /* (1) If the member is a scalar consuming <N> basic machine units, the
1889 * base alignment is <N>.
1891 * (2) If the member is a two- or four-component vector with components
1892 * consuming <N> basic machine units, the base alignment is 2<N> or
1893 * 4<N>, respectively.
1895 * (3) If the member is a three-component vector with components consuming
1896 * <N> basic machine units, the base alignment is 4<N>.
1898 if (this->is_array())
1899 return this->fields
.array
->std430_base_alignment(row_major
);
1901 /* (5) If the member is a column-major matrix with <C> columns and
1902 * <R> rows, the matrix is stored identically to an array of
1903 * <C> column vectors with <R> components each, according to
1906 * (7) If the member is a row-major matrix with <C> columns and <R>
1907 * rows, the matrix is stored identically to an array of <R>
1908 * row vectors with <C> components each, according to rule (4).
1910 if (this->is_matrix()) {
1911 const struct glsl_type
*vec_type
, *array_type
;
1912 int c
= this->matrix_columns
;
1913 int r
= this->vector_elements
;
1916 vec_type
= get_instance(base_type
, c
, 1);
1917 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1919 vec_type
= get_instance(base_type
, r
, 1);
1920 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1923 return array_type
->std430_base_alignment(false);
1926 /* (9) If the member is a structure, the base alignment of the
1927 * structure is <N>, where <N> is the largest base alignment
1928 * value of any of its members, and rounded up to the base
1929 * alignment of a vec4. The individual members of this
1930 * sub-structure are then assigned offsets by applying this set
1931 * of rules recursively, where the base offset of the first
1932 * member of the sub-structure is equal to the aligned offset
1933 * of the structure. The structure may have padding at the end;
1934 * the base offset of the member following the sub-structure is
1935 * rounded up to the next multiple of the base alignment of the
1938 if (this->is_struct()) {
1939 unsigned base_alignment
= 0;
1940 for (unsigned i
= 0; i
< this->length
; i
++) {
1941 bool field_row_major
= row_major
;
1942 const enum glsl_matrix_layout matrix_layout
=
1943 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1944 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1945 field_row_major
= true;
1946 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1947 field_row_major
= false;
1950 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1951 base_alignment
= MAX2(base_alignment
,
1952 field_type
->std430_base_alignment(field_row_major
));
1954 assert(base_alignment
> 0);
1955 return base_alignment
;
1957 assert(!"not reached");
1962 glsl_type::std430_array_stride(bool row_major
) const
1964 unsigned N
= is_64bit() ? 8 : 4;
1966 assert(explicit_stride
== 0);
1968 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1969 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1971 * (3) If the member is a three-component vector with components consuming
1972 * <N> basic machine units, the base alignment is 4<N>.
1974 if (this->is_vector() && this->vector_elements
== 3)
1977 /* By default use std430_size(row_major) */
1978 return this->std430_size(row_major
);
1982 glsl_type::std430_size(bool row_major
) const
1984 unsigned N
= is_64bit() ? 8 : 4;
1986 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1988 * "When using the std430 storage layout, shader storage blocks will be
1989 * laid out in buffer storage identically to uniform and shader storage
1990 * blocks using the std140 layout, except that the base alignment and
1991 * stride of arrays of scalars and vectors in rule 4 and of structures
1992 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1994 if (this->is_scalar() || this->is_vector()) {
1995 assert(this->explicit_stride
== 0);
1996 return this->vector_elements
* N
;
1999 if (this->without_array()->is_matrix()) {
2000 const struct glsl_type
*element_type
;
2001 const struct glsl_type
*vec_type
;
2002 unsigned int array_len
;
2004 if (this->is_array()) {
2005 element_type
= this->without_array();
2006 array_len
= this->arrays_of_arrays_size();
2008 element_type
= this;
2012 assert(element_type
->explicit_stride
== 0);
2015 vec_type
= get_instance(element_type
->base_type
,
2016 element_type
->matrix_columns
, 1);
2018 array_len
*= element_type
->vector_elements
;
2020 vec_type
= get_instance(element_type
->base_type
,
2021 element_type
->vector_elements
, 1);
2022 array_len
*= element_type
->matrix_columns
;
2024 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
2027 return array_type
->std430_size(false);
2030 if (this->is_array()) {
2031 assert(this->explicit_stride
== 0);
2032 if (this->without_array()->is_struct())
2033 return this->arrays_of_arrays_size() *
2034 this->without_array()->std430_size(row_major
);
2036 return this->arrays_of_arrays_size() *
2037 this->without_array()->std430_base_alignment(row_major
);
2040 if (this->is_struct() || this->is_interface()) {
2042 unsigned max_align
= 0;
2044 for (unsigned i
= 0; i
< this->length
; i
++) {
2045 bool field_row_major
= row_major
;
2046 const enum glsl_matrix_layout matrix_layout
=
2047 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
2048 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2049 field_row_major
= true;
2050 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2051 field_row_major
= false;
2054 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
2055 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
2056 size
= glsl_align(size
, align
);
2057 size
+= field_type
->std430_size(field_row_major
);
2059 max_align
= MAX2(align
, max_align
);
2061 size
= glsl_align(size
, max_align
);
2065 assert(!"not reached");
2070 glsl_type::count_attribute_slots(bool is_gl_vertex_input
) const
2072 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2074 * "A scalar input counts the same amount against this limit as a vec4,
2075 * so applications may want to consider packing groups of four
2076 * unrelated float inputs together into a vector to better utilize the
2077 * capabilities of the underlying hardware. A matrix input will use up
2078 * multiple locations. The number of locations used will equal the
2079 * number of columns in the matrix."
2081 * The spec does not explicitly say how arrays are counted. However, it
2082 * should be safe to assume the total number of slots consumed by an array
2083 * is the number of entries in the array multiplied by the number of slots
2084 * consumed by a single element of the array.
2086 * The spec says nothing about how structs are counted, because vertex
2087 * attributes are not allowed to be (or contain) structs. However, Mesa
2088 * allows varying structs, the number of varying slots taken up by a
2089 * varying struct is simply equal to the sum of the number of slots taken
2090 * up by each element.
2092 * Doubles are counted different depending on whether they are vertex
2093 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
2094 * take one location no matter what size they are, otherwise dvec3/4
2095 * take two locations.
2097 switch (this->base_type
) {
2098 case GLSL_TYPE_UINT
:
2100 case GLSL_TYPE_UINT8
:
2101 case GLSL_TYPE_INT8
:
2102 case GLSL_TYPE_UINT16
:
2103 case GLSL_TYPE_INT16
:
2104 case GLSL_TYPE_FLOAT
:
2105 case GLSL_TYPE_FLOAT16
:
2106 case GLSL_TYPE_BOOL
:
2107 case GLSL_TYPE_SAMPLER
:
2108 case GLSL_TYPE_IMAGE
:
2109 return this->matrix_columns
;
2110 case GLSL_TYPE_DOUBLE
:
2111 case GLSL_TYPE_UINT64
:
2112 case GLSL_TYPE_INT64
:
2113 if (this->vector_elements
> 2 && !is_gl_vertex_input
)
2114 return this->matrix_columns
* 2;
2116 return this->matrix_columns
;
2117 case GLSL_TYPE_STRUCT
:
2118 case GLSL_TYPE_INTERFACE
: {
2121 for (unsigned i
= 0; i
< this->length
; i
++) {
2122 const glsl_type
*member_type
= this->fields
.structure
[i
].type
;
2123 size
+= member_type
->count_attribute_slots(is_gl_vertex_input
);
2129 case GLSL_TYPE_ARRAY
: {
2130 const glsl_type
*element
= this->fields
.array
;
2131 return this->length
* element
->count_attribute_slots(is_gl_vertex_input
);
2134 case GLSL_TYPE_SUBROUTINE
:
2137 case GLSL_TYPE_FUNCTION
:
2138 case GLSL_TYPE_ATOMIC_UINT
:
2139 case GLSL_TYPE_VOID
:
2140 case GLSL_TYPE_ERROR
:
2144 assert(!"Unexpected type in count_attribute_slots()");
2150 glsl_type::coordinate_components() const
2154 switch (sampler_dimensionality
) {
2155 case GLSL_SAMPLER_DIM_1D
:
2156 case GLSL_SAMPLER_DIM_BUF
:
2159 case GLSL_SAMPLER_DIM_2D
:
2160 case GLSL_SAMPLER_DIM_RECT
:
2161 case GLSL_SAMPLER_DIM_MS
:
2162 case GLSL_SAMPLER_DIM_EXTERNAL
:
2163 case GLSL_SAMPLER_DIM_SUBPASS
:
2166 case GLSL_SAMPLER_DIM_3D
:
2167 case GLSL_SAMPLER_DIM_CUBE
:
2171 assert(!"Should not get here.");
2176 /* Array textures need an additional component for the array index, except
2177 * for cubemap array images that behave like a 2D array of interleaved
2180 if (sampler_array
&&
2181 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2188 * Declarations of type flyweights (glsl_type::_foo_type) and
2189 * convenience pointers (glsl_type::foo_type).
2192 #define DECL_TYPE(NAME, ...) \
2193 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2194 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2196 #define STRUCT_TYPE(NAME)
2198 #include "compiler/builtin_type_macros.h"
2202 get_struct_type_field_and_pointer_sizes(size_t *s_field_size
,
2203 size_t *s_field_ptrs
)
2205 *s_field_size
= sizeof(glsl_struct_field
);
2207 sizeof(((glsl_struct_field
*)0)->type
) +
2208 sizeof(((glsl_struct_field
*)0)->name
);
2212 encode_type_to_blob(struct blob
*blob
, const glsl_type
*type
)
2217 blob_write_uint32(blob
, 0);
2221 switch (type
->base_type
) {
2222 case GLSL_TYPE_UINT
:
2224 case GLSL_TYPE_FLOAT
:
2225 case GLSL_TYPE_FLOAT16
:
2226 case GLSL_TYPE_DOUBLE
:
2227 case GLSL_TYPE_UINT8
:
2228 case GLSL_TYPE_INT8
:
2229 case GLSL_TYPE_UINT16
:
2230 case GLSL_TYPE_INT16
:
2231 case GLSL_TYPE_UINT64
:
2232 case GLSL_TYPE_INT64
:
2233 case GLSL_TYPE_BOOL
:
2234 encoding
= (type
->base_type
<< 24) |
2235 (type
->interface_row_major
<< 10) |
2236 (type
->vector_elements
<< 4) |
2237 (type
->matrix_columns
);
2238 blob_write_uint32(blob
, encoding
);
2239 blob_write_uint32(blob
, type
->explicit_stride
);
2241 case GLSL_TYPE_SAMPLER
:
2242 encoding
= (type
->base_type
) << 24 |
2243 (type
->sampler_dimensionality
<< 4) |
2244 (type
->sampler_shadow
<< 3) |
2245 (type
->sampler_array
<< 2) |
2246 (type
->sampled_type
);
2248 case GLSL_TYPE_SUBROUTINE
:
2249 encoding
= type
->base_type
<< 24;
2250 blob_write_uint32(blob
, encoding
);
2251 blob_write_string(blob
, type
->name
);
2253 case GLSL_TYPE_IMAGE
:
2254 encoding
= (type
->base_type
) << 24 |
2255 (type
->sampler_dimensionality
<< 3) |
2256 (type
->sampler_array
<< 2) |
2257 (type
->sampled_type
);
2259 case GLSL_TYPE_ATOMIC_UINT
:
2260 encoding
= (type
->base_type
<< 24);
2262 case GLSL_TYPE_ARRAY
:
2263 blob_write_uint32(blob
, (type
->base_type
) << 24);
2264 blob_write_uint32(blob
, type
->length
);
2265 blob_write_uint32(blob
, type
->explicit_stride
);
2266 encode_type_to_blob(blob
, type
->fields
.array
);
2268 case GLSL_TYPE_STRUCT
:
2269 case GLSL_TYPE_INTERFACE
:
2270 blob_write_uint32(blob
, (type
->base_type
) << 24);
2271 blob_write_string(blob
, type
->name
);
2272 blob_write_uint32(blob
, type
->length
);
2274 size_t s_field_size
, s_field_ptrs
;
2275 get_struct_type_field_and_pointer_sizes(&s_field_size
, &s_field_ptrs
);
2277 for (unsigned i
= 0; i
< type
->length
; i
++) {
2278 encode_type_to_blob(blob
, type
->fields
.structure
[i
].type
);
2279 blob_write_string(blob
, type
->fields
.structure
[i
].name
);
2281 /* Write the struct field skipping the pointers */
2282 blob_write_bytes(blob
,
2283 ((char *)&type
->fields
.structure
[i
]) + s_field_ptrs
,
2284 s_field_size
- s_field_ptrs
);
2287 if (type
->is_interface()) {
2288 blob_write_uint32(blob
, type
->interface_packing
);
2289 blob_write_uint32(blob
, type
->interface_row_major
);
2292 case GLSL_TYPE_VOID
:
2293 encoding
= (type
->base_type
<< 24);
2295 case GLSL_TYPE_ERROR
:
2297 assert(!"Cannot encode type!");
2302 blob_write_uint32(blob
, encoding
);
2306 decode_type_from_blob(struct blob_reader
*blob
)
2308 uint32_t u
= blob_read_uint32(blob
);
2314 glsl_base_type base_type
= (glsl_base_type
) (u
>> 24);
2316 switch (base_type
) {
2317 case GLSL_TYPE_UINT
:
2319 case GLSL_TYPE_FLOAT
:
2320 case GLSL_TYPE_FLOAT16
:
2321 case GLSL_TYPE_DOUBLE
:
2322 case GLSL_TYPE_UINT8
:
2323 case GLSL_TYPE_INT8
:
2324 case GLSL_TYPE_UINT16
:
2325 case GLSL_TYPE_INT16
:
2326 case GLSL_TYPE_UINT64
:
2327 case GLSL_TYPE_INT64
:
2328 case GLSL_TYPE_BOOL
: {
2329 unsigned explicit_stride
= blob_read_uint32(blob
);
2330 return glsl_type::get_instance(base_type
, (u
>> 4) & 0x0f, u
& 0x0f,
2331 explicit_stride
, (u
>> 10) & 0x1);
2333 case GLSL_TYPE_SAMPLER
:
2334 return glsl_type::get_sampler_instance((enum glsl_sampler_dim
) ((u
>> 4) & 0x0f),
2337 (glsl_base_type
) ((u
>> 0) & 0x03));
2338 case GLSL_TYPE_SUBROUTINE
:
2339 return glsl_type::get_subroutine_instance(blob_read_string(blob
));
2340 case GLSL_TYPE_IMAGE
:
2341 return glsl_type::get_image_instance((enum glsl_sampler_dim
) ((u
>> 3) & 0x0f),
2343 (glsl_base_type
) ((u
>> 0) & 0x03));
2344 case GLSL_TYPE_ATOMIC_UINT
:
2345 return glsl_type::atomic_uint_type
;
2346 case GLSL_TYPE_ARRAY
: {
2347 unsigned length
= blob_read_uint32(blob
);
2348 unsigned explicit_stride
= blob_read_uint32(blob
);
2349 return glsl_type::get_array_instance(decode_type_from_blob(blob
),
2350 length
, explicit_stride
);
2352 case GLSL_TYPE_STRUCT
:
2353 case GLSL_TYPE_INTERFACE
: {
2354 char *name
= blob_read_string(blob
);
2355 unsigned num_fields
= blob_read_uint32(blob
);
2357 size_t s_field_size
, s_field_ptrs
;
2358 get_struct_type_field_and_pointer_sizes(&s_field_size
, &s_field_ptrs
);
2360 glsl_struct_field
*fields
=
2361 (glsl_struct_field
*) malloc(s_field_size
* num_fields
);
2362 for (unsigned i
= 0; i
< num_fields
; i
++) {
2363 fields
[i
].type
= decode_type_from_blob(blob
);
2364 fields
[i
].name
= blob_read_string(blob
);
2366 blob_copy_bytes(blob
, ((uint8_t *) &fields
[i
]) + s_field_ptrs
,
2367 s_field_size
- s_field_ptrs
);
2371 if (base_type
== GLSL_TYPE_INTERFACE
) {
2372 enum glsl_interface_packing packing
=
2373 (glsl_interface_packing
) blob_read_uint32(blob
);
2374 bool row_major
= blob_read_uint32(blob
);
2375 t
= glsl_type::get_interface_instance(fields
, num_fields
, packing
,
2378 t
= glsl_type::get_record_instance(fields
, num_fields
, name
);
2384 case GLSL_TYPE_VOID
:
2385 return glsl_type::void_type
;
2386 case GLSL_TYPE_ERROR
:
2388 assert(!"Cannot decode type!");