3 * Copyright © 2009 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
32 #include "shader_enums.h"
33 #include "c11/threads.h"
34 #include "util/blob.h"
35 #include "util/format/u_format.h"
36 #include "util/macros.h"
39 #include "main/config.h"
48 struct _mesa_glsl_parse_state
;
49 struct glsl_symbol_table
;
52 glsl_type_singleton_init_or_ref();
55 glsl_type_singleton_decref();
58 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
);
61 glsl_print_type(FILE *f
, const struct glsl_type
*t
);
63 void encode_type_to_blob(struct blob
*blob
, const struct glsl_type
*type
);
65 const struct glsl_type
*decode_type_from_blob(struct blob_reader
*blob
);
67 typedef void (*glsl_type_size_align_func
)(const struct glsl_type
*type
,
68 unsigned *size
, unsigned *align
);
71 /* Note: GLSL_TYPE_UINT, GLSL_TYPE_INT, and GLSL_TYPE_FLOAT must be 0, 1,
72 * and 2 so that they will fit in the 2 bits of glsl_type::sampled_type.
88 GLSL_TYPE_ATOMIC_UINT
,
98 /* Return the bit size of a type. Note that this differs from
99 * glsl_get_bit_size in that it returns 32 bits for bools, whereas at
100 * the NIR level we would want to return 1 bit for bools.
102 static unsigned glsl_base_type_bit_size(enum glsl_base_type type
)
108 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
109 case GLSL_TYPE_SUBROUTINE
:
112 case GLSL_TYPE_FLOAT16
:
113 case GLSL_TYPE_UINT16
:
114 case GLSL_TYPE_INT16
:
117 case GLSL_TYPE_UINT8
:
121 case GLSL_TYPE_DOUBLE
:
122 case GLSL_TYPE_INT64
:
123 case GLSL_TYPE_UINT64
:
124 case GLSL_TYPE_IMAGE
:
125 case GLSL_TYPE_SAMPLER
:
129 /* For GLSL_TYPE_STRUCT etc, it should be ok to return 0. This usually
130 * happens when calling this method through is_64bit and is_16bit
139 static inline bool glsl_base_type_is_16bit(enum glsl_base_type type
)
141 return glsl_base_type_bit_size(type
) == 16;
144 static inline bool glsl_base_type_is_64bit(enum glsl_base_type type
)
146 return glsl_base_type_bit_size(type
) == 64;
149 static inline bool glsl_base_type_is_integer(enum glsl_base_type type
)
151 return type
== GLSL_TYPE_UINT8
||
152 type
== GLSL_TYPE_INT8
||
153 type
== GLSL_TYPE_UINT16
||
154 type
== GLSL_TYPE_INT16
||
155 type
== GLSL_TYPE_UINT
||
156 type
== GLSL_TYPE_INT
||
157 type
== GLSL_TYPE_UINT64
||
158 type
== GLSL_TYPE_INT64
||
159 type
== GLSL_TYPE_BOOL
||
160 type
== GLSL_TYPE_SAMPLER
||
161 type
== GLSL_TYPE_IMAGE
;
164 static inline unsigned int
165 glsl_base_type_get_bit_size(const enum glsl_base_type base_type
)
173 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
174 case GLSL_TYPE_SUBROUTINE
:
177 case GLSL_TYPE_FLOAT16
:
178 case GLSL_TYPE_UINT16
:
179 case GLSL_TYPE_INT16
:
182 case GLSL_TYPE_UINT8
:
186 case GLSL_TYPE_DOUBLE
:
187 case GLSL_TYPE_INT64
:
188 case GLSL_TYPE_UINT64
:
189 case GLSL_TYPE_IMAGE
:
190 case GLSL_TYPE_SAMPLER
:
194 unreachable("unknown base type");
200 static inline enum glsl_base_type
201 glsl_unsigned_base_type_of(enum glsl_base_type type
)
205 return GLSL_TYPE_UINT
;
207 return GLSL_TYPE_UINT8
;
208 case GLSL_TYPE_INT16
:
209 return GLSL_TYPE_UINT16
;
210 case GLSL_TYPE_INT64
:
211 return GLSL_TYPE_UINT64
;
213 assert(type
== GLSL_TYPE_UINT
||
214 type
== GLSL_TYPE_UINT8
||
215 type
== GLSL_TYPE_UINT16
||
216 type
== GLSL_TYPE_UINT64
);
221 enum glsl_sampler_dim
{
222 GLSL_SAMPLER_DIM_1D
= 0,
225 GLSL_SAMPLER_DIM_CUBE
,
226 GLSL_SAMPLER_DIM_RECT
,
227 GLSL_SAMPLER_DIM_BUF
,
228 GLSL_SAMPLER_DIM_EXTERNAL
,
230 GLSL_SAMPLER_DIM_SUBPASS
, /* for vulkan input attachments */
231 GLSL_SAMPLER_DIM_SUBPASS_MS
, /* for multisampled vulkan input attachments */
235 glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim
);
237 enum glsl_matrix_layout
{
239 * The layout of the matrix is inherited from the object containing the
240 * matrix (the top level structure or the uniform block).
242 GLSL_MATRIX_LAYOUT_INHERITED
,
245 * Explicit column-major layout
247 * If a uniform block doesn't have an explicit layout set, it will default
250 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
,
255 GLSL_MATRIX_LAYOUT_ROW_MAJOR
259 GLSL_PRECISION_NONE
= 0,
261 GLSL_PRECISION_MEDIUM
,
269 #include "util/ralloc.h"
270 #include "main/menums.h" /* for gl_texture_index, C++'s enum rules are broken */
274 glsl_base_type base_type
:8;
276 glsl_base_type sampled_type
:8; /**< Type of data returned using this
277 * sampler or image. Only \c
278 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
279 * and \c GLSL_TYPE_UINT are valid.
282 unsigned sampler_dimensionality
:4; /**< \see glsl_sampler_dim */
283 unsigned sampler_shadow
:1;
284 unsigned sampler_array
:1;
285 unsigned interface_packing
:2;
286 unsigned interface_row_major
:1;
289 * For \c GLSL_TYPE_STRUCT this specifies if the struct is packed or not.
291 * Only used for Compute kernels
296 glsl_type() : mem_ctx(NULL
)
298 // Dummy constructor, just for the sake of ASSERT_BITFIELD_SIZE.
303 * \name Vector and matrix element counts
305 * For scalars, each of these values will be 1. For non-numeric types
309 uint8_t vector_elements
; /**< 1, 2, 3, or 4 vector elements. */
310 uint8_t matrix_columns
; /**< 1, 2, 3, or 4 matrix columns. */
314 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
315 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
316 * elements in the structure and the number of values pointed to by
317 * \c fields.structure (below).
322 * Name of the data type
324 * Will never be \c NULL.
329 * Explicit array, matrix, or vector stride. This is used to communicate
330 * explicit array layouts from SPIR-V. Should be 0 if the type has no
333 unsigned explicit_stride
;
336 * Subtype of composite data types.
339 const struct glsl_type
*array
; /**< Type of array elements. */
340 struct glsl_function_param
*parameters
; /**< Parameters to function. */
341 struct glsl_struct_field
*structure
; /**< List of struct fields. */
345 * \name Pointers to various public type singletons
349 #define DECL_TYPE(NAME, ...) \
350 static const glsl_type *const NAME##_type;
352 #define STRUCT_TYPE(NAME) \
353 static const glsl_type *const struct_##NAME##_type;
354 #include "compiler/builtin_type_macros.h"
358 * Convenience accessors for vector types (shorter than get_instance()).
361 static const glsl_type
*vec(unsigned components
, const glsl_type
*const ts
[]);
362 static const glsl_type
*vec(unsigned components
);
363 static const glsl_type
*f16vec(unsigned components
);
364 static const glsl_type
*dvec(unsigned components
);
365 static const glsl_type
*ivec(unsigned components
);
366 static const glsl_type
*uvec(unsigned components
);
367 static const glsl_type
*bvec(unsigned components
);
368 static const glsl_type
*i64vec(unsigned components
);
369 static const glsl_type
*u64vec(unsigned components
);
370 static const glsl_type
*i16vec(unsigned components
);
371 static const glsl_type
*u16vec(unsigned components
);
372 static const glsl_type
*i8vec(unsigned components
);
373 static const glsl_type
*u8vec(unsigned components
);
377 * For numeric and boolean derived types returns the basic scalar type
379 * If the type is a numeric or boolean scalar, vector, or matrix type,
380 * this function gets the scalar type of the individual components. For
381 * all other types, including arrays of numeric or boolean types, the
382 * error type is returned.
384 const glsl_type
*get_base_type() const;
387 * Get the basic scalar type which this type aggregates.
389 * If the type is a numeric or boolean scalar, vector, or matrix, or an
390 * array of any of those, this function gets the scalar type of the
391 * individual components. For structs and arrays of structs, this function
392 * returns the struct type. For samplers and arrays of samplers, this
393 * function returns the sampler type.
395 const glsl_type
*get_scalar_type() const;
398 * Gets the "bare" type without any decorations or layout information.
400 const glsl_type
*get_bare_type() const;
403 * Gets the float16 version of this type.
405 const glsl_type
*get_float16_type() const;
408 * Gets the int16 version of this type.
410 const glsl_type
*get_int16_type() const;
413 * Gets the uint16 version of this type.
415 const glsl_type
*get_uint16_type() const;
418 * Get the instance of a built-in scalar, vector, or matrix type
420 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
422 unsigned explicit_stride
= 0,
423 bool row_major
= false);
426 * Get the instance of a sampler type
428 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
431 glsl_base_type type
);
433 static const glsl_type
*get_image_instance(enum glsl_sampler_dim dim
,
434 bool array
, glsl_base_type type
);
437 * Get the instance of an array type
439 static const glsl_type
*get_array_instance(const glsl_type
*base
,
441 unsigned explicit_stride
= 0);
444 * Get the instance of a record type
446 static const glsl_type
*get_struct_instance(const glsl_struct_field
*fields
,
449 bool packed
= false);
452 * Get the instance of an interface block type
454 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
456 enum glsl_interface_packing packing
,
458 const char *block_name
);
461 * Get the instance of an subroutine type
463 static const glsl_type
*get_subroutine_instance(const char *subroutine_name
);
466 * Get the instance of a function type
468 static const glsl_type
*get_function_instance(const struct glsl_type
*return_type
,
469 const glsl_function_param
*parameters
,
470 unsigned num_params
);
473 * Get the type resulting from a multiplication of \p type_a * \p type_b
475 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
476 const glsl_type
*type_b
);
479 * Query the total number of scalars that make up a scalar, vector or matrix
481 unsigned components() const
483 return vector_elements
* matrix_columns
;
487 * Calculate the number of components slots required to hold this type
489 * This is used to determine how many uniform or varying locations a type
492 unsigned component_slots() const;
495 * Calculate offset between the base location of the struct in
496 * uniform storage and a struct member.
497 * For the initial call, length is the index of the member to find the
500 unsigned struct_location_offset(unsigned length
) const;
503 * Calculate the number of unique values from glGetUniformLocation for the
504 * elements of the type.
506 * This is used to allocate slots in the UniformRemapTable, the amount of
507 * locations may not match with actual used storage space by the driver.
509 unsigned uniform_locations() const;
512 * Used to count the number of varyings contained in the type ignoring
513 * innermost array elements.
515 unsigned varying_count() const;
518 * Calculate the number of vec4 slots required to hold this type.
520 * This is the underlying recursive type_size function for
521 * count_attribute_slots() (vertex inputs and varyings) but also for
522 * gallium's !PIPE_CAP_PACKED_UNIFORMS case.
524 unsigned count_vec4_slots(bool is_gl_vertex_input
, bool bindless
) const;
527 * Calculate the number of vec4 slots required to hold this type.
529 * This is the underlying recursive type_size function for
530 * gallium's PIPE_CAP_PACKED_UNIFORMS case.
532 unsigned count_dword_slots(bool bindless
) const;
535 * Calculate the number of attribute slots required to hold this type
537 * This implements the language rules of GLSL 1.50 for counting the number
538 * of slots used by a vertex attribute. It also determines the number of
539 * varying slots the type will use up in the absence of varying packing
540 * (and thus, it can be used to measure the number of varying slots used by
541 * the varyings that are generated by lower_packed_varyings).
543 * For vertex shader attributes - doubles only take one slot.
544 * For inter-shader varyings - dvec3/dvec4 take two slots.
546 * Vulkan doesn’t make this distinction so the argument should always be
549 unsigned count_attribute_slots(bool is_gl_vertex_input
) const {
550 return count_vec4_slots(is_gl_vertex_input
, true);
554 * Alignment in bytes of the start of this type in a std140 uniform
557 unsigned std140_base_alignment(bool row_major
) const;
559 /** Size in bytes of this type in a std140 uniform block.
561 * Note that this is not GL_UNIFORM_SIZE (which is the number of
562 * elements in the array)
564 unsigned std140_size(bool row_major
) const;
567 * Gets an explicitly laid out type with the std140 layout.
569 const glsl_type
*get_explicit_std140_type(bool row_major
) const;
572 * Alignment in bytes of the start of this type in a std430 shader
575 unsigned std430_base_alignment(bool row_major
) const;
578 * Calculate array stride in bytes of this type in a std430 shader storage
581 unsigned std430_array_stride(bool row_major
) const;
584 * Size in bytes of this type in a std430 shader storage block.
586 * Note that this is not GL_BUFFER_SIZE
588 unsigned std430_size(bool row_major
) const;
591 * Gets an explicitly laid out type with the std430 layout.
593 const glsl_type
*get_explicit_std430_type(bool row_major
) const;
596 * Gets an explicitly laid out interface type.
598 const glsl_type
*get_explicit_interface_type(bool supports_std430
) const;
600 /** Returns an explicitly laid out type given a type and size/align func
602 * The size/align func is only called for scalar and vector types and the
603 * returned type is otherwise laid out in the natural way as follows:
605 * - Arrays and matrices have a stride of ALIGN(elem_size, elem_align).
607 * - Structure types have their elements in-order and as tightly packed as
608 * possible following the alignment required by the size/align func.
610 * - All composite types (structures, matrices, and arrays) have an
611 * alignment equal to the highest alighment of any member of the composite.
613 * The types returned by this function are likely not suitable for most UBO
614 * or SSBO layout because they do not add the extra array and substructure
615 * alignment that is required by std140 and std430.
617 const glsl_type
*get_explicit_type_for_size_align(glsl_type_size_align_func type_info
,
618 unsigned *size
, unsigned *align
) const;
621 * Alignment in bytes of the start of this type in OpenCL memory.
623 unsigned cl_alignment() const;
626 * Size in bytes of this type in OpenCL memory
628 unsigned cl_size() const;
631 * Size in bytes of this type based on its explicit data.
633 * When using SPIR-V shaders (ARB_gl_spirv), memory layouts are expressed
634 * through explicit offset, stride and matrix layout, so the size
635 * can/should be computed used those values.
637 * Note that the value returned by this method is only correct if such
638 * values are set, so only with SPIR-V shaders. Should not be used with
641 unsigned explicit_size(bool align_to_stride
=false) const;
644 * \brief Can this type be implicitly converted to another?
646 * \return True if the types are identical or if this type can be converted
647 * to \c desired according to Section 4.1.10 of the GLSL spec.
650 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
651 * Implicit Conversions:
653 * In some situations, an expression and its type will be implicitly
654 * converted to a different type. The following table shows all allowed
655 * implicit conversions:
657 * Type of expression | Can be implicitly converted to
658 * --------------------------------------------------
671 * There are no implicit array or structure conversions. For example,
672 * an array of int cannot be implicitly converted to an array of float.
673 * There are no implicit conversions between signed and unsigned
677 bool can_implicitly_convert_to(const glsl_type
*desired
,
678 _mesa_glsl_parse_state
*state
) const;
681 * Query whether or not a type is a scalar (non-vector and non-matrix).
683 bool is_scalar() const
685 return (vector_elements
== 1)
686 && (base_type
>= GLSL_TYPE_UINT
)
687 && (base_type
<= GLSL_TYPE_IMAGE
);
691 * Query whether or not a type is a vector
693 bool is_vector() const
695 return (vector_elements
> 1)
696 && (matrix_columns
== 1)
697 && (base_type
>= GLSL_TYPE_UINT
)
698 && (base_type
<= GLSL_TYPE_BOOL
);
702 * Query whether or not a type is a matrix
704 bool is_matrix() const
706 /* GLSL only has float matrices. */
707 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
||
708 base_type
== GLSL_TYPE_DOUBLE
||
709 base_type
== GLSL_TYPE_FLOAT16
);
713 * Query whether or not a type is a non-array numeric type
715 bool is_numeric() const
717 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_INT64
);
721 * Query whether or not a type is an integer.
723 bool is_integer() const
725 return glsl_base_type_is_integer(base_type
);
729 * Query whether or not a type is a 16-bit integer.
731 bool is_integer_16() const
733 return base_type
== GLSL_TYPE_UINT16
|| base_type
== GLSL_TYPE_INT16
;
737 * Query whether or not a type is an 32-bit integer.
739 bool is_integer_32() const
741 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
745 * Query whether or not a type is a 64-bit integer.
747 bool is_integer_64() const
749 return base_type
== GLSL_TYPE_UINT64
|| base_type
== GLSL_TYPE_INT64
;
753 * Query whether or not a type is a 32-bit or 64-bit integer
755 bool is_integer_32_64() const
757 return is_integer_32() || is_integer_64();
761 * Query whether or not a type is a 16-bit or 32-bit integer
763 bool is_integer_16_32() const
765 return is_integer_16() || is_integer_32() || is_integer_64();
769 * Query whether or not a type is a 16-bit, 32-bit or 64-bit integer
771 bool is_integer_16_32_64() const
773 return is_integer_16() || is_integer_32() || is_integer_64();
777 * Query whether or not type is an integral type, or for struct and array
778 * types, contains an integral type.
780 bool contains_integer() const;
783 * Query whether or not type is a double type, or for struct, interface and
784 * array types, contains a double type.
786 bool contains_double() const;
789 * Query whether or not type is a 64-bit type, or for struct, interface and
790 * array types, contains a double type.
792 bool contains_64bit() const;
795 * Query whether or not a type is a float type
797 bool is_float() const
799 return base_type
== GLSL_TYPE_FLOAT
;
803 * Query whether or not a type is a half-float or float type
805 bool is_float_16_32() const
807 return base_type
== GLSL_TYPE_FLOAT16
|| is_float();
811 * Query whether or not a type is a half-float, float or double
813 bool is_float_16_32_64() const
815 return base_type
== GLSL_TYPE_FLOAT16
|| is_float() || is_double();
819 * Query whether or not a type is a float or double
821 bool is_float_32_64() const
823 return is_float() || is_double();
826 bool is_int_16_32_64() const
828 return base_type
== GLSL_TYPE_INT16
||
829 base_type
== GLSL_TYPE_INT
||
830 base_type
== GLSL_TYPE_INT64
;
833 bool is_uint_16_32_64() const
835 return base_type
== GLSL_TYPE_UINT16
||
836 base_type
== GLSL_TYPE_UINT
||
837 base_type
== GLSL_TYPE_UINT64
;
840 bool is_int_16_32() const
842 return base_type
== GLSL_TYPE_INT
||
843 base_type
== GLSL_TYPE_INT16
;
846 bool is_uint_16_32() const
848 return base_type
== GLSL_TYPE_UINT
||
849 base_type
== GLSL_TYPE_UINT16
;
853 * Query whether or not a type is a double type
855 bool is_double() const
857 return base_type
== GLSL_TYPE_DOUBLE
;
861 * Query whether a 64-bit type takes two slots.
863 bool is_dual_slot() const
865 return is_64bit() && vector_elements
> 2;
869 * Query whether or not a type is 64-bit
871 bool is_64bit() const
873 return glsl_base_type_is_64bit(base_type
);
877 * Query whether or not a type is 16-bit
879 bool is_16bit() const
881 return glsl_base_type_is_16bit(base_type
);
885 * Query whether or not a type is 32-bit
887 bool is_32bit() const
889 return base_type
== GLSL_TYPE_UINT
||
890 base_type
== GLSL_TYPE_INT
||
891 base_type
== GLSL_TYPE_FLOAT
;
895 * Query whether or not a type is a non-array boolean type
897 bool is_boolean() const
899 return base_type
== GLSL_TYPE_BOOL
;
903 * Query whether or not a type is a sampler
905 bool is_sampler() const
907 return base_type
== GLSL_TYPE_SAMPLER
;
911 * Query whether or not type is a sampler, or for struct, interface and
912 * array types, contains a sampler.
914 bool contains_sampler() const;
917 * Query whether or not type is an array or for struct, interface and
918 * array types, contains an array.
920 bool contains_array() const;
923 * Get the Mesa texture target index for a sampler type.
925 gl_texture_index
sampler_index() const;
928 * Query whether or not type is an image, or for struct, interface and
929 * array types, contains an image.
931 bool contains_image() const;
934 * Query whether or not a type is an image
936 bool is_image() const
938 return base_type
== GLSL_TYPE_IMAGE
;
942 * Query whether or not a type is an array
944 bool is_array() const
946 return base_type
== GLSL_TYPE_ARRAY
;
949 bool is_array_of_arrays() const
951 return is_array() && fields
.array
->is_array();
955 * Query whether or not a type is a record
957 bool is_struct() const
959 return base_type
== GLSL_TYPE_STRUCT
;
963 * Query whether or not a type is an interface
965 bool is_interface() const
967 return base_type
== GLSL_TYPE_INTERFACE
;
971 * Query whether or not a type is the void type singleton.
975 return base_type
== GLSL_TYPE_VOID
;
979 * Query whether or not a type is the error type singleton.
981 bool is_error() const
983 return base_type
== GLSL_TYPE_ERROR
;
987 * Query if a type is unnamed/anonymous (named by the parser)
990 bool is_subroutine() const
992 return base_type
== GLSL_TYPE_SUBROUTINE
;
994 bool contains_subroutine() const;
996 bool is_anonymous() const
998 return !strncmp(name
, "#anon", 5);
1002 * Get the type stripped of any arrays
1005 * Pointer to the type of elements of the first non-array type for array
1006 * types, or pointer to itself for non-array types.
1008 const glsl_type
*without_array() const
1010 const glsl_type
*t
= this;
1012 while (t
->is_array())
1013 t
= t
->fields
.array
;
1019 * Return the total number of elements in an array including the elements
1020 * in arrays of arrays.
1022 unsigned arrays_of_arrays_size() const
1027 unsigned size
= length
;
1028 const glsl_type
*base_type
= fields
.array
;
1030 while (base_type
->is_array()) {
1031 size
= size
* base_type
->length
;
1032 base_type
= base_type
->fields
.array
;
1038 * Return bit size for this type.
1040 unsigned bit_size() const
1042 return glsl_base_type_bit_size(this->base_type
);
1047 * Query whether or not a type is an atomic_uint.
1049 bool is_atomic_uint() const
1051 return base_type
== GLSL_TYPE_ATOMIC_UINT
;
1055 * Return the amount of atomic counter storage required for a type.
1057 unsigned atomic_size() const
1059 if (is_atomic_uint())
1060 return ATOMIC_COUNTER_SIZE
;
1061 else if (is_array())
1062 return length
* fields
.array
->atomic_size();
1068 * Return whether a type contains any atomic counters.
1070 bool contains_atomic() const
1072 return atomic_size() > 0;
1076 * Return whether a type contains any opaque types.
1078 bool contains_opaque() const;
1081 * Query the full type of a matrix row
1084 * If the type is not a matrix, \c glsl_type::error_type is returned.
1085 * Otherwise a type matching the rows of the matrix is returned.
1087 const glsl_type
*row_type() const
1092 if (explicit_stride
&& !interface_row_major
)
1093 return get_instance(base_type
, matrix_columns
, 1, explicit_stride
);
1095 return get_instance(base_type
, matrix_columns
, 1);
1099 * Query the full type of a matrix column
1102 * If the type is not a matrix, \c glsl_type::error_type is returned.
1103 * Otherwise a type matching the columns of the matrix is returned.
1105 const glsl_type
*column_type() const
1110 if (explicit_stride
&& interface_row_major
)
1111 return get_instance(base_type
, vector_elements
, 1, explicit_stride
);
1113 return get_instance(base_type
, vector_elements
, 1);
1117 * Get the type of a structure field
1120 * Pointer to the type of the named field. If the type is not a structure
1121 * or the named field does not exist, \c glsl_type::error_type is returned.
1123 const glsl_type
*field_type(const char *name
) const;
1126 * Get the location of a field within a record type
1128 int field_index(const char *name
) const;
1131 * Query the number of elements in an array type
1134 * The number of elements in the array for array types or -1 for non-array
1135 * types. If the number of elements in the array has not yet been declared,
1138 int array_size() const
1140 return is_array() ? length
: -1;
1144 * Query whether the array size for all dimensions has been declared.
1146 bool is_unsized_array() const
1148 return is_array() && length
== 0;
1152 * Return the number of coordinate components needed for this
1153 * sampler or image type.
1155 * This is based purely on the sampler's dimensionality. For example, this
1156 * returns 1 for sampler1D, and 3 for sampler2DArray.
1158 * Note that this is often different than actual coordinate type used in
1159 * a texturing built-in function, since those pack additional values (such
1160 * as the shadow comparator or projector) into the coordinate type.
1162 int coordinate_components() const;
1165 * Compares whether this type matches another type without taking into
1166 * account the precision in structures.
1168 * This is applied recursively so that structures containing structure
1169 * members can also ignore the precision.
1171 bool compare_no_precision(const glsl_type
*b
) const;
1174 * Compare a record type against another record type.
1176 * This is useful for matching record types declared on the same shader
1177 * stage as well as across different shader stages.
1178 * The option to not match name is needed for matching record types
1179 * declared across different shader stages.
1180 * The option to not match locations is to deal with places where the
1181 * same struct is defined in a block which has a location set on it.
1183 bool record_compare(const glsl_type
*b
, bool match_name
,
1184 bool match_locations
= true,
1185 bool match_precision
= true) const;
1188 * Get the type interface packing.
1190 enum glsl_interface_packing
get_interface_packing() const
1192 return (enum glsl_interface_packing
)interface_packing
;
1196 * Get the type interface packing used internally. For shared and packing
1197 * layouts this is implementation defined.
1199 enum glsl_interface_packing
get_internal_ifc_packing(bool std430_supported
) const
1201 enum glsl_interface_packing packing
= this->get_interface_packing();
1202 if (packing
== GLSL_INTERFACE_PACKING_STD140
||
1203 (!std430_supported
&&
1204 (packing
== GLSL_INTERFACE_PACKING_SHARED
||
1205 packing
== GLSL_INTERFACE_PACKING_PACKED
))) {
1206 return GLSL_INTERFACE_PACKING_STD140
;
1208 assert(packing
== GLSL_INTERFACE_PACKING_STD430
||
1209 (std430_supported
&&
1210 (packing
== GLSL_INTERFACE_PACKING_SHARED
||
1211 packing
== GLSL_INTERFACE_PACKING_PACKED
)));
1212 return GLSL_INTERFACE_PACKING_STD430
;
1217 * Check if the type interface is row major
1219 bool get_interface_row_major() const
1221 return (bool) interface_row_major
;
1228 static mtx_t hash_mutex
;
1231 * ralloc context for the type itself.
1235 /** Constructor for vector and matrix types */
1236 glsl_type(GLenum gl_type
,
1237 glsl_base_type base_type
, unsigned vector_elements
,
1238 unsigned matrix_columns
, const char *name
,
1239 unsigned explicit_stride
= 0, bool row_major
= false);
1241 /** Constructor for sampler or image types */
1242 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
1243 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
1244 glsl_base_type type
, const char *name
);
1246 /** Constructor for record types */
1247 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
1248 const char *name
, bool packed
= false);
1250 /** Constructor for interface types */
1251 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
1252 enum glsl_interface_packing packing
,
1253 bool row_major
, const char *name
);
1255 /** Constructor for interface types */
1256 glsl_type(const glsl_type
*return_type
,
1257 const glsl_function_param
*params
, unsigned num_params
);
1259 /** Constructors for array types */
1260 glsl_type(const glsl_type
*array
, unsigned length
, unsigned explicit_stride
);
1262 /** Constructor for subroutine types */
1263 glsl_type(const char *name
);
1265 /** Hash table containing the known explicit matrix and vector types. */
1266 static struct hash_table
*explicit_matrix_types
;
1268 /** Hash table containing the known array types. */
1269 static struct hash_table
*array_types
;
1271 /** Hash table containing the known struct types. */
1272 static struct hash_table
*struct_types
;
1274 /** Hash table containing the known interface types. */
1275 static struct hash_table
*interface_types
;
1277 /** Hash table containing the known subroutine types. */
1278 static struct hash_table
*subroutine_types
;
1280 /** Hash table containing the known function types. */
1281 static struct hash_table
*function_types
;
1283 static bool record_key_compare(const void *a
, const void *b
);
1284 static unsigned record_key_hash(const void *key
);
1287 * \name Built-in type flyweights
1291 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
1293 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
1294 #include "compiler/builtin_type_macros.h"
1298 * \name Friend functions.
1300 * These functions are friends because they must have C linkage and the
1301 * need to call various private methods or access various private static
1305 friend void glsl_type_singleton_init_or_ref(void);
1306 friend void glsl_type_singleton_decref(void);
1307 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
1313 #endif /* __cplusplus */
1315 struct glsl_struct_field
{
1316 const struct glsl_type
*type
;
1320 * For interface blocks, gl_varying_slot corresponding to the input/output
1321 * if this is a built-in input/output (i.e. a member of the built-in
1322 * gl_PerVertex interface block); -1 otherwise.
1324 * Ignored for structs.
1329 * For interface blocks, members may have an explicit byte offset
1330 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
1332 * Unless used for xfb_offset this field is ignored for structs.
1337 * For interface blocks, members may define a transform feedback buffer;
1343 * For interface blocks, members may define a transform feedback stride;
1348 * Layout format, applicable to image variables only.
1350 enum pipe_format image_format
;
1355 * For interface blocks, the interpolation mode (as in
1356 * ir_variable::interpolation). 0 otherwise.
1358 unsigned interpolation
:3;
1361 * For interface blocks, 1 if this variable uses centroid interpolation (as
1362 * in ir_variable::centroid). 0 otherwise.
1364 unsigned centroid
:1;
1367 * For interface blocks, 1 if this variable uses sample interpolation (as
1368 * in ir_variable::sample). 0 otherwise.
1373 * Layout of the matrix. Uses glsl_matrix_layout values.
1375 unsigned matrix_layout
:2;
1378 * For interface blocks, 1 if this variable is a per-patch input or output
1379 * (as in ir_variable::patch). 0 otherwise.
1384 * Precision qualifier
1386 unsigned precision
:2;
1389 * Memory qualifiers, applicable to buffer variables defined in shader
1390 * storage buffer objects (SSBOs)
1392 unsigned memory_read_only
:1;
1393 unsigned memory_write_only
:1;
1394 unsigned memory_coherent
:1;
1395 unsigned memory_volatile
:1;
1396 unsigned memory_restrict
:1;
1399 * Any of the xfb_* qualifiers trigger the shader to be in transform
1400 * feedback mode so we need to keep track of whether the buffer was
1401 * explicitly set or if its just been assigned the default global value.
1403 unsigned explicit_xfb_buffer
:1;
1405 unsigned implicit_sized_array
:1;
1410 #define DEFAULT_CONSTRUCTORS(_type, _name) \
1411 type(_type), name(_name), location(-1), offset(-1), xfb_buffer(0), \
1412 xfb_stride(0), image_format(PIPE_FORMAT_NONE), flags(0) \
1414 glsl_struct_field(const struct glsl_type *_type,
1417 : DEFAULT_CONSTRUCTORS(_type
, _name
)
1419 matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
1420 precision
= _precision
;
1423 glsl_struct_field(const struct glsl_type
*_type
, const char *_name
)
1424 : DEFAULT_CONSTRUCTORS(_type
, _name
)
1426 matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
1427 precision
= GLSL_PRECISION_NONE
;
1431 : DEFAULT_CONSTRUCTORS(NULL
, NULL
)
1433 matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
1434 precision
= GLSL_PRECISION_NONE
;
1436 #undef DEFAULT_CONSTRUCTORS
1440 struct glsl_function_param
{
1441 const struct glsl_type
*type
;
1447 static inline unsigned int
1448 glsl_align(unsigned int a
, unsigned int align
)
1450 return (a
+ align
- 1) / align
* align
;
1453 #endif /* GLSL_TYPES_H */