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.
31 #include "shader_enums.h"
32 #include "c11/threads.h"
33 #include "util/blob.h"
34 #include "util/format/u_format.h"
35 #include "util/macros.h"
38 #include "main/config.h"
47 struct _mesa_glsl_parse_state
;
48 struct glsl_symbol_table
;
51 glsl_type_singleton_init_or_ref();
54 glsl_type_singleton_decref();
57 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
);
59 void encode_type_to_blob(struct blob
*blob
, const struct glsl_type
*type
);
61 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 */
234 enum glsl_matrix_layout
{
236 * The layout of the matrix is inherited from the object containing the
237 * matrix (the top level structure or the uniform block).
239 GLSL_MATRIX_LAYOUT_INHERITED
,
242 * Explicit column-major layout
244 * If a uniform block doesn't have an explicit layout set, it will default
247 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
,
252 GLSL_MATRIX_LAYOUT_ROW_MAJOR
256 GLSL_PRECISION_NONE
= 0,
258 GLSL_PRECISION_MEDIUM
,
264 #include "util/ralloc.h"
265 #include "main/menums.h" /* for gl_texture_index, C++'s enum rules are broken */
269 glsl_base_type base_type
:8;
271 glsl_base_type sampled_type
:8; /**< Type of data returned using this
272 * sampler or image. Only \c
273 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
274 * and \c GLSL_TYPE_UINT are valid.
277 unsigned sampler_dimensionality
:4; /**< \see glsl_sampler_dim */
278 unsigned sampler_shadow
:1;
279 unsigned sampler_array
:1;
280 unsigned interface_packing
:2;
281 unsigned interface_row_major
:1;
284 * For \c GLSL_TYPE_STRUCT this specifies if the struct is packed or not.
286 * Only used for Compute kernels
291 glsl_type() : mem_ctx(NULL
)
293 // Dummy constructor, just for the sake of ASSERT_BITFIELD_SIZE.
298 * \name Vector and matrix element counts
300 * For scalars, each of these values will be 1. For non-numeric types
304 uint8_t vector_elements
; /**< 1, 2, 3, or 4 vector elements. */
305 uint8_t matrix_columns
; /**< 1, 2, 3, or 4 matrix columns. */
309 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
310 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
311 * elements in the structure and the number of values pointed to by
312 * \c fields.structure (below).
317 * Name of the data type
319 * Will never be \c NULL.
324 * Explicit array, matrix, or vector stride. This is used to communicate
325 * explicit array layouts from SPIR-V. Should be 0 if the type has no
328 unsigned explicit_stride
;
331 * Subtype of composite data types.
334 const struct glsl_type
*array
; /**< Type of array elements. */
335 struct glsl_function_param
*parameters
; /**< Parameters to function. */
336 struct glsl_struct_field
*structure
; /**< List of struct fields. */
340 * \name Pointers to various public type singletons
344 #define DECL_TYPE(NAME, ...) \
345 static const glsl_type *const NAME##_type;
347 #define STRUCT_TYPE(NAME) \
348 static const glsl_type *const struct_##NAME##_type;
349 #include "compiler/builtin_type_macros.h"
353 * Convenience accessors for vector types (shorter than get_instance()).
356 static const glsl_type
*vec(unsigned components
, const glsl_type
*const ts
[]);
357 static const glsl_type
*vec(unsigned components
);
358 static const glsl_type
*f16vec(unsigned components
);
359 static const glsl_type
*dvec(unsigned components
);
360 static const glsl_type
*ivec(unsigned components
);
361 static const glsl_type
*uvec(unsigned components
);
362 static const glsl_type
*bvec(unsigned components
);
363 static const glsl_type
*i64vec(unsigned components
);
364 static const glsl_type
*u64vec(unsigned components
);
365 static const glsl_type
*i16vec(unsigned components
);
366 static const glsl_type
*u16vec(unsigned components
);
367 static const glsl_type
*i8vec(unsigned components
);
368 static const glsl_type
*u8vec(unsigned components
);
372 * For numeric and boolean derived types returns the basic scalar type
374 * If the type is a numeric or boolean scalar, vector, or matrix type,
375 * this function gets the scalar type of the individual components. For
376 * all other types, including arrays of numeric or boolean types, the
377 * error type is returned.
379 const glsl_type
*get_base_type() const;
382 * Get the basic scalar type which this type aggregates.
384 * If the type is a numeric or boolean scalar, vector, or matrix, or an
385 * array of any of those, this function gets the scalar type of the
386 * individual components. For structs and arrays of structs, this function
387 * returns the struct type. For samplers and arrays of samplers, this
388 * function returns the sampler type.
390 const glsl_type
*get_scalar_type() const;
393 * Gets the "bare" type without any decorations or layout information.
395 const glsl_type
*get_bare_type() const;
398 * Gets the float16 version of this type.
400 const glsl_type
*get_float16_type() const;
403 * Get the instance of a built-in scalar, vector, or matrix type
405 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
407 unsigned explicit_stride
= 0,
408 bool row_major
= false);
411 * Get the instance of a sampler type
413 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
416 glsl_base_type type
);
418 static const glsl_type
*get_image_instance(enum glsl_sampler_dim dim
,
419 bool array
, glsl_base_type type
);
422 * Get the instance of an array type
424 static const glsl_type
*get_array_instance(const glsl_type
*base
,
426 unsigned explicit_stride
= 0);
429 * Get the instance of a record type
431 static const glsl_type
*get_struct_instance(const glsl_struct_field
*fields
,
434 bool packed
= false);
437 * Get the instance of an interface block type
439 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
441 enum glsl_interface_packing packing
,
443 const char *block_name
);
446 * Get the instance of an subroutine type
448 static const glsl_type
*get_subroutine_instance(const char *subroutine_name
);
451 * Get the instance of a function type
453 static const glsl_type
*get_function_instance(const struct glsl_type
*return_type
,
454 const glsl_function_param
*parameters
,
455 unsigned num_params
);
458 * Get the type resulting from a multiplication of \p type_a * \p type_b
460 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
461 const glsl_type
*type_b
);
464 * Query the total number of scalars that make up a scalar, vector or matrix
466 unsigned components() const
468 return vector_elements
* matrix_columns
;
472 * Calculate the number of components slots required to hold this type
474 * This is used to determine how many uniform or varying locations a type
477 unsigned component_slots() const;
480 * Calculate offset between the base location of the struct in
481 * uniform storage and a struct member.
482 * For the initial call, length is the index of the member to find the
485 unsigned struct_location_offset(unsigned length
) const;
488 * Calculate the number of unique values from glGetUniformLocation for the
489 * elements of the type.
491 * This is used to allocate slots in the UniformRemapTable, the amount of
492 * locations may not match with actual used storage space by the driver.
494 unsigned uniform_locations() const;
497 * Used to count the number of varyings contained in the type ignoring
498 * innermost array elements.
500 unsigned varying_count() const;
503 * Calculate the number of vec4 slots required to hold this type.
505 * This is the underlying recursive type_size function for
506 * count_attribute_slots() (vertex inputs and varyings) but also for
507 * gallium's !PIPE_CAP_PACKED_UNIFORMS case.
509 unsigned count_vec4_slots(bool is_gl_vertex_input
, bool bindless
) const;
512 * Calculate the number of vec4 slots required to hold this type.
514 * This is the underlying recursive type_size function for
515 * gallium's PIPE_CAP_PACKED_UNIFORMS case.
517 unsigned count_dword_slots(bool bindless
) const;
520 * Calculate the number of attribute slots required to hold this type
522 * This implements the language rules of GLSL 1.50 for counting the number
523 * of slots used by a vertex attribute. It also determines the number of
524 * varying slots the type will use up in the absence of varying packing
525 * (and thus, it can be used to measure the number of varying slots used by
526 * the varyings that are generated by lower_packed_varyings).
528 * For vertex shader attributes - doubles only take one slot.
529 * For inter-shader varyings - dvec3/dvec4 take two slots.
531 * Vulkan doesn’t make this distinction so the argument should always be
534 unsigned count_attribute_slots(bool is_gl_vertex_input
) const {
535 return count_vec4_slots(is_gl_vertex_input
, true);
539 * Alignment in bytes of the start of this type in a std140 uniform
542 unsigned std140_base_alignment(bool row_major
) const;
544 /** Size in bytes of this type in a std140 uniform block.
546 * Note that this is not GL_UNIFORM_SIZE (which is the number of
547 * elements in the array)
549 unsigned std140_size(bool row_major
) const;
552 * Gets an explicitly laid out type with the std140 layout.
554 const glsl_type
*get_explicit_std140_type(bool row_major
) const;
557 * Alignment in bytes of the start of this type in a std430 shader
560 unsigned std430_base_alignment(bool row_major
) const;
563 * Calculate array stride in bytes of this type in a std430 shader storage
566 unsigned std430_array_stride(bool row_major
) const;
569 * Size in bytes of this type in a std430 shader storage block.
571 * Note that this is not GL_BUFFER_SIZE
573 unsigned std430_size(bool row_major
) const;
576 * Gets an explicitly laid out type with the std430 layout.
578 const glsl_type
*get_explicit_std430_type(bool row_major
) const;
581 * Gets an explicitly laid out interface type.
583 const glsl_type
*get_explicit_interface_type(bool supports_std430
) const;
585 /** Returns an explicitly laid out type given a type and size/align func
587 * The size/align func is only called for scalar and vector types and the
588 * returned type is otherwise laid out in the natural way as follows:
590 * - Arrays and matrices have a stride of ALIGN(elem_size, elem_align).
592 * - Structure types have their elements in-order and as tightly packed as
593 * possible following the alignment required by the size/align func.
595 * - All composite types (structures, matrices, and arrays) have an
596 * alignment equal to the highest alighment of any member of the composite.
598 * The types returned by this function are likely not suitable for most UBO
599 * or SSBO layout because they do not add the extra array and substructure
600 * alignment that is required by std140 and std430.
602 const glsl_type
*get_explicit_type_for_size_align(glsl_type_size_align_func type_info
,
603 unsigned *size
, unsigned *align
) const;
606 * Alignment in bytes of the start of this type in OpenCL memory.
608 unsigned cl_alignment() const;
611 * Size in bytes of this type in OpenCL memory
613 unsigned cl_size() const;
616 * Size in bytes of this type based on its explicit data.
618 * When using SPIR-V shaders (ARB_gl_spirv), memory layouts are expressed
619 * through explicit offset, stride and matrix layout, so the size
620 * can/should be computed used those values.
622 * Note that the value returned by this method is only correct if such
623 * values are set, so only with SPIR-V shaders. Should not be used with
626 unsigned explicit_size(bool align_to_stride
=false) const;
629 * \brief Can this type be implicitly converted to another?
631 * \return True if the types are identical or if this type can be converted
632 * to \c desired according to Section 4.1.10 of the GLSL spec.
635 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
636 * Implicit Conversions:
638 * In some situations, an expression and its type will be implicitly
639 * converted to a different type. The following table shows all allowed
640 * implicit conversions:
642 * Type of expression | Can be implicitly converted to
643 * --------------------------------------------------
656 * There are no implicit array or structure conversions. For example,
657 * an array of int cannot be implicitly converted to an array of float.
658 * There are no implicit conversions between signed and unsigned
662 bool can_implicitly_convert_to(const glsl_type
*desired
,
663 _mesa_glsl_parse_state
*state
) const;
666 * Query whether or not a type is a scalar (non-vector and non-matrix).
668 bool is_scalar() const
670 return (vector_elements
== 1)
671 && (base_type
>= GLSL_TYPE_UINT
)
672 && (base_type
<= GLSL_TYPE_IMAGE
);
676 * Query whether or not a type is a vector
678 bool is_vector() const
680 return (vector_elements
> 1)
681 && (matrix_columns
== 1)
682 && (base_type
>= GLSL_TYPE_UINT
)
683 && (base_type
<= GLSL_TYPE_BOOL
);
687 * Query whether or not a type is a matrix
689 bool is_matrix() const
691 /* GLSL only has float matrices. */
692 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
||
693 base_type
== GLSL_TYPE_DOUBLE
||
694 base_type
== GLSL_TYPE_FLOAT16
);
698 * Query whether or not a type is a non-array numeric type
700 bool is_numeric() const
702 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_INT64
);
706 * Query whether or not a type is an integer.
708 bool is_integer() const
710 return glsl_base_type_is_integer(base_type
);
714 * Query whether or not a type is an 32-bit integer.
716 bool is_integer_32() const
718 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
722 * Query whether or not a type is a 64-bit integer.
724 bool is_integer_64() const
726 return base_type
== GLSL_TYPE_UINT64
|| base_type
== GLSL_TYPE_INT64
;
730 * Query whether or not a type is a 32-bit or 64-bit integer
732 bool is_integer_32_64() const
734 return is_integer_32() || is_integer_64();
738 * Query whether or not type is an integral type, or for struct and array
739 * types, contains an integral type.
741 bool contains_integer() const;
744 * Query whether or not type is a double type, or for struct, interface and
745 * array types, contains a double type.
747 bool contains_double() const;
750 * Query whether or not type is a 64-bit type, or for struct, interface and
751 * array types, contains a double type.
753 bool contains_64bit() const;
756 * Query whether or not a type is a float type
758 bool is_float() const
760 return base_type
== GLSL_TYPE_FLOAT
;
764 * Query whether or not a type is a double type
766 bool is_double() const
768 return base_type
== GLSL_TYPE_DOUBLE
;
772 * Query whether a 64-bit type takes two slots.
774 bool is_dual_slot() const
776 return is_64bit() && vector_elements
> 2;
780 * Query whether or not a type is 64-bit
782 bool is_64bit() const
784 return glsl_base_type_is_64bit(base_type
);
788 * Query whether or not a type is 16-bit
790 bool is_16bit() const
792 return glsl_base_type_is_16bit(base_type
);
796 * Query whether or not a type is 32-bit
798 bool is_32bit() const
800 return base_type
== GLSL_TYPE_UINT
||
801 base_type
== GLSL_TYPE_INT
||
802 base_type
== GLSL_TYPE_FLOAT
;
806 * Query whether or not a type is a non-array boolean type
808 bool is_boolean() const
810 return base_type
== GLSL_TYPE_BOOL
;
814 * Query whether or not a type is a sampler
816 bool is_sampler() const
818 return base_type
== GLSL_TYPE_SAMPLER
;
822 * Query whether or not type is a sampler, or for struct, interface and
823 * array types, contains a sampler.
825 bool contains_sampler() const;
828 * Query whether or not type is an array or for struct, interface and
829 * array types, contains an array.
831 bool contains_array() const;
834 * Get the Mesa texture target index for a sampler type.
836 gl_texture_index
sampler_index() const;
839 * Query whether or not type is an image, or for struct, interface and
840 * array types, contains an image.
842 bool contains_image() const;
845 * Query whether or not a type is an image
847 bool is_image() const
849 return base_type
== GLSL_TYPE_IMAGE
;
853 * Query whether or not a type is an array
855 bool is_array() const
857 return base_type
== GLSL_TYPE_ARRAY
;
860 bool is_array_of_arrays() const
862 return is_array() && fields
.array
->is_array();
866 * Query whether or not a type is a record
868 bool is_struct() const
870 return base_type
== GLSL_TYPE_STRUCT
;
874 * Query whether or not a type is an interface
876 bool is_interface() const
878 return base_type
== GLSL_TYPE_INTERFACE
;
882 * Query whether or not a type is the void type singleton.
886 return base_type
== GLSL_TYPE_VOID
;
890 * Query whether or not a type is the error type singleton.
892 bool is_error() const
894 return base_type
== GLSL_TYPE_ERROR
;
898 * Query if a type is unnamed/anonymous (named by the parser)
901 bool is_subroutine() const
903 return base_type
== GLSL_TYPE_SUBROUTINE
;
905 bool contains_subroutine() const;
907 bool is_anonymous() const
909 return !strncmp(name
, "#anon", 5);
913 * Get the type stripped of any arrays
916 * Pointer to the type of elements of the first non-array type for array
917 * types, or pointer to itself for non-array types.
919 const glsl_type
*without_array() const
921 const glsl_type
*t
= this;
923 while (t
->is_array())
930 * Return the total number of elements in an array including the elements
931 * in arrays of arrays.
933 unsigned arrays_of_arrays_size() const
938 unsigned size
= length
;
939 const glsl_type
*base_type
= fields
.array
;
941 while (base_type
->is_array()) {
942 size
= size
* base_type
->length
;
943 base_type
= base_type
->fields
.array
;
949 * Return bit size for this type.
951 unsigned bit_size() const
953 return glsl_base_type_bit_size(this->base_type
);
958 * Query whether or not a type is an atomic_uint.
960 bool is_atomic_uint() const
962 return base_type
== GLSL_TYPE_ATOMIC_UINT
;
966 * Return the amount of atomic counter storage required for a type.
968 unsigned atomic_size() const
970 if (is_atomic_uint())
971 return ATOMIC_COUNTER_SIZE
;
973 return length
* fields
.array
->atomic_size();
979 * Return whether a type contains any atomic counters.
981 bool contains_atomic() const
983 return atomic_size() > 0;
987 * Return whether a type contains any opaque types.
989 bool contains_opaque() const;
992 * Query the full type of a matrix row
995 * If the type is not a matrix, \c glsl_type::error_type is returned.
996 * Otherwise a type matching the rows of the matrix is returned.
998 const glsl_type
*row_type() const
1003 if (explicit_stride
&& !interface_row_major
)
1004 return get_instance(base_type
, matrix_columns
, 1, explicit_stride
);
1006 return get_instance(base_type
, matrix_columns
, 1);
1010 * Query the full type of a matrix column
1013 * If the type is not a matrix, \c glsl_type::error_type is returned.
1014 * Otherwise a type matching the columns of the matrix is returned.
1016 const glsl_type
*column_type() const
1021 if (explicit_stride
&& interface_row_major
)
1022 return get_instance(base_type
, vector_elements
, 1, explicit_stride
);
1024 return get_instance(base_type
, vector_elements
, 1);
1028 * Get the type of a structure field
1031 * Pointer to the type of the named field. If the type is not a structure
1032 * or the named field does not exist, \c glsl_type::error_type is returned.
1034 const glsl_type
*field_type(const char *name
) const;
1037 * Get the location of a field within a record type
1039 int field_index(const char *name
) const;
1042 * Query the number of elements in an array type
1045 * The number of elements in the array for array types or -1 for non-array
1046 * types. If the number of elements in the array has not yet been declared,
1049 int array_size() const
1051 return is_array() ? length
: -1;
1055 * Query whether the array size for all dimensions has been declared.
1057 bool is_unsized_array() const
1059 return is_array() && length
== 0;
1063 * Return the number of coordinate components needed for this
1064 * sampler or image type.
1066 * This is based purely on the sampler's dimensionality. For example, this
1067 * returns 1 for sampler1D, and 3 for sampler2DArray.
1069 * Note that this is often different than actual coordinate type used in
1070 * a texturing built-in function, since those pack additional values (such
1071 * as the shadow comparator or projector) into the coordinate type.
1073 int coordinate_components() const;
1076 * Compares whether this type matches another type without taking into
1077 * account the precision in structures.
1079 * This is applied recursively so that structures containing structure
1080 * members can also ignore the precision.
1082 bool compare_no_precision(const glsl_type
*b
) const;
1085 * Compare a record type against another record type.
1087 * This is useful for matching record types declared on the same shader
1088 * stage as well as across different shader stages.
1089 * The option to not match name is needed for matching record types
1090 * declared across different shader stages.
1091 * The option to not match locations is to deal with places where the
1092 * same struct is defined in a block which has a location set on it.
1094 bool record_compare(const glsl_type
*b
, bool match_name
,
1095 bool match_locations
= true,
1096 bool match_precision
= true) const;
1099 * Get the type interface packing.
1101 enum glsl_interface_packing
get_interface_packing() const
1103 return (enum glsl_interface_packing
)interface_packing
;
1107 * Get the type interface packing used internally. For shared and packing
1108 * layouts this is implementation defined.
1110 enum glsl_interface_packing
get_internal_ifc_packing(bool std430_supported
) const
1112 enum glsl_interface_packing packing
= this->get_interface_packing();
1113 if (packing
== GLSL_INTERFACE_PACKING_STD140
||
1114 (!std430_supported
&&
1115 (packing
== GLSL_INTERFACE_PACKING_SHARED
||
1116 packing
== GLSL_INTERFACE_PACKING_PACKED
))) {
1117 return GLSL_INTERFACE_PACKING_STD140
;
1119 assert(packing
== GLSL_INTERFACE_PACKING_STD430
||
1120 (std430_supported
&&
1121 (packing
== GLSL_INTERFACE_PACKING_SHARED
||
1122 packing
== GLSL_INTERFACE_PACKING_PACKED
)));
1123 return GLSL_INTERFACE_PACKING_STD430
;
1128 * Check if the type interface is row major
1130 bool get_interface_row_major() const
1132 return (bool) interface_row_major
;
1139 static mtx_t hash_mutex
;
1142 * ralloc context for the type itself.
1146 /** Constructor for vector and matrix types */
1147 glsl_type(GLenum gl_type
,
1148 glsl_base_type base_type
, unsigned vector_elements
,
1149 unsigned matrix_columns
, const char *name
,
1150 unsigned explicit_stride
= 0, bool row_major
= false);
1152 /** Constructor for sampler or image types */
1153 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
1154 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
1155 glsl_base_type type
, const char *name
);
1157 /** Constructor for record types */
1158 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
1159 const char *name
, bool packed
= false);
1161 /** Constructor for interface types */
1162 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
1163 enum glsl_interface_packing packing
,
1164 bool row_major
, const char *name
);
1166 /** Constructor for interface types */
1167 glsl_type(const glsl_type
*return_type
,
1168 const glsl_function_param
*params
, unsigned num_params
);
1170 /** Constructors for array types */
1171 glsl_type(const glsl_type
*array
, unsigned length
, unsigned explicit_stride
);
1173 /** Constructor for subroutine types */
1174 glsl_type(const char *name
);
1176 /** Hash table containing the known explicit matrix and vector types. */
1177 static struct hash_table
*explicit_matrix_types
;
1179 /** Hash table containing the known array types. */
1180 static struct hash_table
*array_types
;
1182 /** Hash table containing the known struct types. */
1183 static struct hash_table
*struct_types
;
1185 /** Hash table containing the known interface types. */
1186 static struct hash_table
*interface_types
;
1188 /** Hash table containing the known subroutine types. */
1189 static struct hash_table
*subroutine_types
;
1191 /** Hash table containing the known function types. */
1192 static struct hash_table
*function_types
;
1194 static bool record_key_compare(const void *a
, const void *b
);
1195 static unsigned record_key_hash(const void *key
);
1198 * \name Built-in type flyweights
1202 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
1204 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
1205 #include "compiler/builtin_type_macros.h"
1209 * \name Friend functions.
1211 * These functions are friends because they must have C linkage and the
1212 * need to call various private methods or access various private static
1216 friend void glsl_type_singleton_init_or_ref(void);
1217 friend void glsl_type_singleton_decref(void);
1218 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
1224 #endif /* __cplusplus */
1226 struct glsl_struct_field
{
1227 const struct glsl_type
*type
;
1231 * For interface blocks, gl_varying_slot corresponding to the input/output
1232 * if this is a built-in input/output (i.e. a member of the built-in
1233 * gl_PerVertex interface block); -1 otherwise.
1235 * Ignored for structs.
1240 * For interface blocks, members may have an explicit byte offset
1241 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
1243 * Unless used for xfb_offset this field is ignored for structs.
1248 * For interface blocks, members may define a transform feedback buffer;
1254 * For interface blocks, members may define a transform feedback stride;
1260 * For interface blocks, the interpolation mode (as in
1261 * ir_variable::interpolation). 0 otherwise.
1263 unsigned interpolation
:3;
1266 * For interface blocks, 1 if this variable uses centroid interpolation (as
1267 * in ir_variable::centroid). 0 otherwise.
1269 unsigned centroid
:1;
1272 * For interface blocks, 1 if this variable uses sample interpolation (as
1273 * in ir_variable::sample). 0 otherwise.
1278 * Layout of the matrix. Uses glsl_matrix_layout values.
1280 unsigned matrix_layout
:2;
1283 * For interface blocks, 1 if this variable is a per-patch input or output
1284 * (as in ir_variable::patch). 0 otherwise.
1289 * Precision qualifier
1291 unsigned precision
:2;
1294 * Memory qualifiers, applicable to buffer variables defined in shader
1295 * storage buffer objects (SSBOs)
1297 unsigned memory_read_only
:1;
1298 unsigned memory_write_only
:1;
1299 unsigned memory_coherent
:1;
1300 unsigned memory_volatile
:1;
1301 unsigned memory_restrict
:1;
1304 * Layout format, applicable to image variables only.
1306 enum pipe_format image_format
;
1309 * Any of the xfb_* qualifiers trigger the shader to be in transform
1310 * feedback mode so we need to keep track of whether the buffer was
1311 * explicitly set or if its just been assigned the default global value.
1313 unsigned explicit_xfb_buffer
:1;
1315 unsigned implicit_sized_array
:1;
1317 #define DEFAULT_CONSTRUCTORS(_type, _precision, _name) \
1318 type(_type), name(_name), location(-1), offset(-1), xfb_buffer(0), \
1319 xfb_stride(0), interpolation(0), centroid(0), \
1320 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED), patch(0), \
1321 precision(_precision), memory_read_only(0), \
1322 memory_write_only(0), memory_coherent(0), memory_volatile(0), \
1323 memory_restrict(0), image_format(PIPE_FORMAT_NONE), \
1324 explicit_xfb_buffer(0), \
1325 implicit_sized_array(0)
1327 glsl_struct_field(const struct glsl_type
*_type
,
1330 : DEFAULT_CONSTRUCTORS(_type
, _precision
, _name
)
1335 glsl_struct_field(const struct glsl_type
*_type
, const char *_name
)
1336 : DEFAULT_CONSTRUCTORS(_type
, GLSL_PRECISION_NONE
, _name
)
1342 : DEFAULT_CONSTRUCTORS(NULL
, GLSL_PRECISION_NONE
, NULL
)
1346 #undef DEFAULT_CONSTRUCTORS
1350 struct glsl_function_param
{
1351 const struct glsl_type
*type
;
1357 static inline unsigned int
1358 glsl_align(unsigned int a
, unsigned int align
)
1360 return (a
+ align
- 1) / align
* align
;
1363 #endif /* GLSL_TYPES_H */