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/macros.h"
37 #include "main/config.h"
46 struct _mesa_glsl_parse_state
;
47 struct glsl_symbol_table
;
50 glsl_type_singleton_init_or_ref();
53 glsl_type_singleton_decref();
56 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
);
58 void encode_type_to_blob(struct blob
*blob
, const struct glsl_type
*type
);
60 const struct glsl_type
*decode_type_from_blob(struct blob_reader
*blob
);
66 typedef void (*glsl_type_size_align_func
)(const struct glsl_type
*type
,
67 unsigned *size
, unsigned *align
);
70 /* Note: GLSL_TYPE_UINT, GLSL_TYPE_INT, and GLSL_TYPE_FLOAT must be 0, 1,
71 * and 2 so that they will fit in the 2 bits of glsl_type::sampled_type.
87 GLSL_TYPE_ATOMIC_UINT
,
97 /* Return the bit size of a type. Note that this differs from
98 * glsl_get_bit_size in that it returns 32 bits for bools, whereas at
99 * the NIR level we would want to return 1 bit for bools.
101 static unsigned glsl_base_type_bit_size(enum glsl_base_type type
)
107 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
108 case GLSL_TYPE_SUBROUTINE
:
111 case GLSL_TYPE_FLOAT16
:
112 case GLSL_TYPE_UINT16
:
113 case GLSL_TYPE_INT16
:
116 case GLSL_TYPE_UINT8
:
120 case GLSL_TYPE_DOUBLE
:
121 case GLSL_TYPE_INT64
:
122 case GLSL_TYPE_UINT64
:
123 case GLSL_TYPE_IMAGE
:
124 case GLSL_TYPE_SAMPLER
:
128 /* For GLSL_TYPE_STRUCT etc, it should be ok to return 0. This usually
129 * happens when calling this method through is_64bit and is_16bit
138 static inline bool glsl_base_type_is_16bit(enum glsl_base_type type
)
140 return glsl_base_type_bit_size(type
) == 16;
143 static inline bool glsl_base_type_is_64bit(enum glsl_base_type type
)
145 return glsl_base_type_bit_size(type
) == 64;
148 static inline bool glsl_base_type_is_integer(enum glsl_base_type type
)
150 return type
== GLSL_TYPE_UINT8
||
151 type
== GLSL_TYPE_INT8
||
152 type
== GLSL_TYPE_UINT16
||
153 type
== GLSL_TYPE_INT16
||
154 type
== GLSL_TYPE_UINT
||
155 type
== GLSL_TYPE_INT
||
156 type
== GLSL_TYPE_UINT64
||
157 type
== GLSL_TYPE_INT64
||
158 type
== GLSL_TYPE_BOOL
||
159 type
== GLSL_TYPE_SAMPLER
||
160 type
== GLSL_TYPE_IMAGE
;
163 static inline unsigned int
164 glsl_base_type_get_bit_size(const enum glsl_base_type base_type
)
172 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
173 case GLSL_TYPE_SUBROUTINE
:
176 case GLSL_TYPE_FLOAT16
:
177 case GLSL_TYPE_UINT16
:
178 case GLSL_TYPE_INT16
:
181 case GLSL_TYPE_UINT8
:
185 case GLSL_TYPE_DOUBLE
:
186 case GLSL_TYPE_INT64
:
187 case GLSL_TYPE_UINT64
:
188 case GLSL_TYPE_IMAGE
:
189 case GLSL_TYPE_SAMPLER
:
193 unreachable("unknown base type");
199 enum glsl_sampler_dim
{
200 GLSL_SAMPLER_DIM_1D
= 0,
203 GLSL_SAMPLER_DIM_CUBE
,
204 GLSL_SAMPLER_DIM_RECT
,
205 GLSL_SAMPLER_DIM_BUF
,
206 GLSL_SAMPLER_DIM_EXTERNAL
,
208 GLSL_SAMPLER_DIM_SUBPASS
, /* for vulkan input attachments */
209 GLSL_SAMPLER_DIM_SUBPASS_MS
, /* for multisampled vulkan input attachments */
212 enum glsl_matrix_layout
{
214 * The layout of the matrix is inherited from the object containing the
215 * matrix (the top level structure or the uniform block).
217 GLSL_MATRIX_LAYOUT_INHERITED
,
220 * Explicit column-major layout
222 * If a uniform block doesn't have an explicit layout set, it will default
225 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
,
230 GLSL_MATRIX_LAYOUT_ROW_MAJOR
234 GLSL_PRECISION_NONE
= 0,
236 GLSL_PRECISION_MEDIUM
,
242 #include "util/ralloc.h"
243 #include "main/menums.h" /* for gl_texture_index, C++'s enum rules are broken */
247 glsl_base_type base_type
:8;
249 glsl_base_type sampled_type
:8; /**< Type of data returned using this
250 * sampler or image. Only \c
251 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
252 * and \c GLSL_TYPE_UINT are valid.
255 unsigned sampler_dimensionality
:4; /**< \see glsl_sampler_dim */
256 unsigned sampler_shadow
:1;
257 unsigned sampler_array
:1;
258 unsigned interface_packing
:2;
259 unsigned interface_row_major
:1;
262 * For \c GLSL_TYPE_STRUCT this specifies if the struct is packed or not.
264 * Only used for Compute kernels
269 glsl_type() : mem_ctx(NULL
)
271 // Dummy constructor, just for the sake of ASSERT_BITFIELD_SIZE.
276 * \name Vector and matrix element counts
278 * For scalars, each of these values will be 1. For non-numeric types
282 uint8_t vector_elements
; /**< 1, 2, 3, or 4 vector elements. */
283 uint8_t matrix_columns
; /**< 1, 2, 3, or 4 matrix columns. */
287 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
288 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
289 * elements in the structure and the number of values pointed to by
290 * \c fields.structure (below).
295 * Name of the data type
297 * Will never be \c NULL.
302 * Explicit array, matrix, or vector stride. This is used to communicate
303 * explicit array layouts from SPIR-V. Should be 0 if the type has no
306 unsigned explicit_stride
;
309 * Subtype of composite data types.
312 const struct glsl_type
*array
; /**< Type of array elements. */
313 struct glsl_function_param
*parameters
; /**< Parameters to function. */
314 struct glsl_struct_field
*structure
; /**< List of struct fields. */
318 * \name Pointers to various public type singletons
322 #define DECL_TYPE(NAME, ...) \
323 static const glsl_type *const NAME##_type;
325 #define STRUCT_TYPE(NAME) \
326 static const glsl_type *const struct_##NAME##_type;
327 #include "compiler/builtin_type_macros.h"
331 * Convenience accessors for vector types (shorter than get_instance()).
334 static const glsl_type
*vec(unsigned components
, const glsl_type
*const ts
[]);
335 static const glsl_type
*vec(unsigned components
);
336 static const glsl_type
*f16vec(unsigned components
);
337 static const glsl_type
*dvec(unsigned components
);
338 static const glsl_type
*ivec(unsigned components
);
339 static const glsl_type
*uvec(unsigned components
);
340 static const glsl_type
*bvec(unsigned components
);
341 static const glsl_type
*i64vec(unsigned components
);
342 static const glsl_type
*u64vec(unsigned components
);
343 static const glsl_type
*i16vec(unsigned components
);
344 static const glsl_type
*u16vec(unsigned components
);
345 static const glsl_type
*i8vec(unsigned components
);
346 static const glsl_type
*u8vec(unsigned components
);
350 * For numeric and boolean derived types returns the basic scalar type
352 * If the type is a numeric or boolean scalar, vector, or matrix type,
353 * this function gets the scalar type of the individual components. For
354 * all other types, including arrays of numeric or boolean types, the
355 * error type is returned.
357 const glsl_type
*get_base_type() const;
360 * Get the basic scalar type which this type aggregates.
362 * If the type is a numeric or boolean scalar, vector, or matrix, or an
363 * array of any of those, this function gets the scalar type of the
364 * individual components. For structs and arrays of structs, this function
365 * returns the struct type. For samplers and arrays of samplers, this
366 * function returns the sampler type.
368 const glsl_type
*get_scalar_type() const;
371 * Gets the "bare" type without any decorations or layout information.
373 const glsl_type
*get_bare_type() const;
376 * Get the instance of a built-in scalar, vector, or matrix type
378 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
380 unsigned explicit_stride
= 0,
381 bool row_major
= false);
384 * Get the instance of a sampler type
386 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
389 glsl_base_type type
);
391 static const glsl_type
*get_image_instance(enum glsl_sampler_dim dim
,
392 bool array
, glsl_base_type type
);
395 * Get the instance of an array type
397 static const glsl_type
*get_array_instance(const glsl_type
*base
,
399 unsigned explicit_stride
= 0);
402 * Get the instance of a record type
404 static const glsl_type
*get_struct_instance(const glsl_struct_field
*fields
,
407 bool packed
= false);
410 * Get the instance of an interface block type
412 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
414 enum glsl_interface_packing packing
,
416 const char *block_name
);
419 * Get the instance of an subroutine type
421 static const glsl_type
*get_subroutine_instance(const char *subroutine_name
);
424 * Get the instance of a function type
426 static const glsl_type
*get_function_instance(const struct glsl_type
*return_type
,
427 const glsl_function_param
*parameters
,
428 unsigned num_params
);
431 * Get the type resulting from a multiplication of \p type_a * \p type_b
433 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
434 const glsl_type
*type_b
);
437 * Query the total number of scalars that make up a scalar, vector or matrix
439 unsigned components() const
441 return vector_elements
* matrix_columns
;
445 * Calculate the number of components slots required to hold this type
447 * This is used to determine how many uniform or varying locations a type
450 unsigned component_slots() const;
453 * Calculate offset between the base location of the struct in
454 * uniform storage and a struct member.
455 * For the initial call, length is the index of the member to find the
458 unsigned struct_location_offset(unsigned length
) const;
461 * Calculate the number of unique values from glGetUniformLocation for the
462 * elements of the type.
464 * This is used to allocate slots in the UniformRemapTable, the amount of
465 * locations may not match with actual used storage space by the driver.
467 unsigned uniform_locations() const;
470 * Used to count the number of varyings contained in the type ignoring
471 * innermost array elements.
473 unsigned varying_count() const;
476 * Calculate the number of vec4 slots required to hold this type.
478 * This is the underlying recursive type_size function for
479 * count_attribute_slots() (vertex inputs and varyings) but also for
480 * gallium's !PIPE_CAP_PACKED_UNIFORMS case.
482 unsigned count_vec4_slots(bool is_gl_vertex_input
, bool bindless
) const;
485 * Calculate the number of attribute slots required to hold this type
487 * This implements the language rules of GLSL 1.50 for counting the number
488 * of slots used by a vertex attribute. It also determines the number of
489 * varying slots the type will use up in the absence of varying packing
490 * (and thus, it can be used to measure the number of varying slots used by
491 * the varyings that are generated by lower_packed_varyings).
493 * For vertex shader attributes - doubles only take one slot.
494 * For inter-shader varyings - dvec3/dvec4 take two slots.
496 * Vulkan doesn’t make this distinction so the argument should always be
499 unsigned count_attribute_slots(bool is_gl_vertex_input
) const {
500 return count_vec4_slots(is_gl_vertex_input
, true);
504 * Alignment in bytes of the start of this type in a std140 uniform
507 unsigned std140_base_alignment(bool row_major
) const;
509 /** Size in bytes of this type in a std140 uniform block.
511 * Note that this is not GL_UNIFORM_SIZE (which is the number of
512 * elements in the array)
514 unsigned std140_size(bool row_major
) const;
517 * Gets an explicitly laid out type with the std140 layout.
519 const glsl_type
*get_explicit_std140_type(bool row_major
) const;
522 * Alignment in bytes of the start of this type in a std430 shader
525 unsigned std430_base_alignment(bool row_major
) const;
528 * Calculate array stride in bytes of this type in a std430 shader storage
531 unsigned std430_array_stride(bool row_major
) const;
534 * Size in bytes of this type in a std430 shader storage block.
536 * Note that this is not GL_BUFFER_SIZE
538 unsigned std430_size(bool row_major
) const;
541 * Gets an explicitly laid out type with the std430 layout.
543 const glsl_type
*get_explicit_std430_type(bool row_major
) const;
546 * Gets an explicitly laid out interface type.
548 const glsl_type
*get_explicit_interface_type(bool supports_std430
) const;
550 /** Returns an explicitly laid out type given a type and size/align func
552 * The size/align func is only called for scalar and vector types and the
553 * returned type is otherwise laid out in the natural way as follows:
555 * - Arrays and matrices have a stride of ALIGN(elem_size, elem_align).
557 * - Structure types have their elements in-order and as tightly packed as
558 * possible following the alignment required by the size/align func.
560 * - All composite types (structures, matrices, and arrays) have an
561 * alignment equal to the highest alighment of any member of the composite.
563 * The types returned by this function are likely not suitable for most UBO
564 * or SSBO layout because they do not add the extra array and substructure
565 * alignment that is required by std140 and std430.
567 const glsl_type
*get_explicit_type_for_size_align(glsl_type_size_align_func type_info
,
568 unsigned *size
, unsigned *align
) const;
571 * Alignment in bytes of the start of this type in OpenCL memory.
573 unsigned cl_alignment() const;
576 * Size in bytes of this type in OpenCL memory
578 unsigned cl_size() const;
581 * Size in bytes of this type based on its explicit data.
583 * When using SPIR-V shaders (ARB_gl_spirv), memory layouts are expressed
584 * through explicit offset, stride and matrix layout, so the size
585 * can/should be computed used those values.
587 * Note that the value returned by this method is only correct if such
588 * values are set, so only with SPIR-V shaders. Should not be used with
591 unsigned explicit_size(bool align_to_stride
=false) const;
594 * \brief Can this type be implicitly converted to another?
596 * \return True if the types are identical or if this type can be converted
597 * to \c desired according to Section 4.1.10 of the GLSL spec.
600 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
601 * Implicit Conversions:
603 * In some situations, an expression and its type will be implicitly
604 * converted to a different type. The following table shows all allowed
605 * implicit conversions:
607 * Type of expression | Can be implicitly converted to
608 * --------------------------------------------------
621 * There are no implicit array or structure conversions. For example,
622 * an array of int cannot be implicitly converted to an array of float.
623 * There are no implicit conversions between signed and unsigned
627 bool can_implicitly_convert_to(const glsl_type
*desired
,
628 _mesa_glsl_parse_state
*state
) const;
631 * Query whether or not a type is a scalar (non-vector and non-matrix).
633 bool is_scalar() const
635 return (vector_elements
== 1)
636 && (base_type
>= GLSL_TYPE_UINT
)
637 && (base_type
<= GLSL_TYPE_IMAGE
);
641 * Query whether or not a type is a vector
643 bool is_vector() const
645 return (vector_elements
> 1)
646 && (matrix_columns
== 1)
647 && (base_type
>= GLSL_TYPE_UINT
)
648 && (base_type
<= GLSL_TYPE_BOOL
);
652 * Query whether or not a type is a matrix
654 bool is_matrix() const
656 /* GLSL only has float matrices. */
657 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
||
658 base_type
== GLSL_TYPE_DOUBLE
||
659 base_type
== GLSL_TYPE_FLOAT16
);
663 * Query whether or not a type is a non-array numeric type
665 bool is_numeric() const
667 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_INT64
);
671 * Query whether or not a type is an integer.
673 bool is_integer() const
675 return glsl_base_type_is_integer(base_type
);
679 * Query whether or not a type is an 32-bit integer.
681 bool is_integer_32() const
683 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
687 * Query whether or not a type is a 64-bit integer.
689 bool is_integer_64() const
691 return base_type
== GLSL_TYPE_UINT64
|| base_type
== GLSL_TYPE_INT64
;
695 * Query whether or not a type is a 32-bit or 64-bit integer
697 bool is_integer_32_64() const
699 return is_integer_32() || is_integer_64();
703 * Query whether or not type is an integral type, or for struct and array
704 * types, contains an integral type.
706 bool contains_integer() const;
709 * Query whether or not type is a double type, or for struct, interface and
710 * array types, contains a double type.
712 bool contains_double() const;
715 * Query whether or not type is a 64-bit type, or for struct, interface and
716 * array types, contains a double type.
718 bool contains_64bit() const;
721 * Query whether or not a type is a float type
723 bool is_float() const
725 return base_type
== GLSL_TYPE_FLOAT
;
729 * Query whether or not a type is a double type
731 bool is_double() const
733 return base_type
== GLSL_TYPE_DOUBLE
;
737 * Query whether a 64-bit type takes two slots.
739 bool is_dual_slot() const
741 return is_64bit() && vector_elements
> 2;
745 * Query whether or not a type is 64-bit
747 bool is_64bit() const
749 return glsl_base_type_is_64bit(base_type
);
753 * Query whether or not a type is 16-bit
755 bool is_16bit() const
757 return glsl_base_type_is_16bit(base_type
);
761 * Query whether or not a type is 32-bit
763 bool is_32bit() const
765 return base_type
== GLSL_TYPE_UINT
||
766 base_type
== GLSL_TYPE_INT
||
767 base_type
== GLSL_TYPE_FLOAT
;
771 * Query whether or not a type is a non-array boolean type
773 bool is_boolean() const
775 return base_type
== GLSL_TYPE_BOOL
;
779 * Query whether or not a type is a sampler
781 bool is_sampler() const
783 return base_type
== GLSL_TYPE_SAMPLER
;
787 * Query whether or not type is a sampler, or for struct, interface and
788 * array types, contains a sampler.
790 bool contains_sampler() const;
793 * Query whether or not type is an array or for struct, interface and
794 * array types, contains an array.
796 bool contains_array() const;
799 * Get the Mesa texture target index for a sampler type.
801 gl_texture_index
sampler_index() const;
804 * Query whether or not type is an image, or for struct, interface and
805 * array types, contains an image.
807 bool contains_image() const;
810 * Query whether or not a type is an image
812 bool is_image() const
814 return base_type
== GLSL_TYPE_IMAGE
;
818 * Query whether or not a type is an array
820 bool is_array() const
822 return base_type
== GLSL_TYPE_ARRAY
;
825 bool is_array_of_arrays() const
827 return is_array() && fields
.array
->is_array();
831 * Query whether or not a type is a record
833 bool is_struct() const
835 return base_type
== GLSL_TYPE_STRUCT
;
839 * Query whether or not a type is an interface
841 bool is_interface() const
843 return base_type
== GLSL_TYPE_INTERFACE
;
847 * Query whether or not a type is the void type singleton.
851 return base_type
== GLSL_TYPE_VOID
;
855 * Query whether or not a type is the error type singleton.
857 bool is_error() const
859 return base_type
== GLSL_TYPE_ERROR
;
863 * Query if a type is unnamed/anonymous (named by the parser)
866 bool is_subroutine() const
868 return base_type
== GLSL_TYPE_SUBROUTINE
;
870 bool contains_subroutine() const;
872 bool is_anonymous() const
874 return !strncmp(name
, "#anon", 5);
878 * Get the type stripped of any arrays
881 * Pointer to the type of elements of the first non-array type for array
882 * types, or pointer to itself for non-array types.
884 const glsl_type
*without_array() const
886 const glsl_type
*t
= this;
888 while (t
->is_array())
895 * Return the total number of elements in an array including the elements
896 * in arrays of arrays.
898 unsigned arrays_of_arrays_size() const
903 unsigned size
= length
;
904 const glsl_type
*base_type
= fields
.array
;
906 while (base_type
->is_array()) {
907 size
= size
* base_type
->length
;
908 base_type
= base_type
->fields
.array
;
914 * Return bit size for this type.
916 unsigned bit_size() const
918 return glsl_base_type_bit_size(this->base_type
);
923 * Query whether or not a type is an atomic_uint.
925 bool is_atomic_uint() const
927 return base_type
== GLSL_TYPE_ATOMIC_UINT
;
931 * Return the amount of atomic counter storage required for a type.
933 unsigned atomic_size() const
935 if (is_atomic_uint())
936 return ATOMIC_COUNTER_SIZE
;
938 return length
* fields
.array
->atomic_size();
944 * Return whether a type contains any atomic counters.
946 bool contains_atomic() const
948 return atomic_size() > 0;
952 * Return whether a type contains any opaque types.
954 bool contains_opaque() const;
957 * Query the full type of a matrix row
960 * If the type is not a matrix, \c glsl_type::error_type is returned.
961 * Otherwise a type matching the rows of the matrix is returned.
963 const glsl_type
*row_type() const
968 if (explicit_stride
&& !interface_row_major
)
969 return get_instance(base_type
, matrix_columns
, 1, explicit_stride
);
971 return get_instance(base_type
, matrix_columns
, 1);
975 * Query the full type of a matrix column
978 * If the type is not a matrix, \c glsl_type::error_type is returned.
979 * Otherwise a type matching the columns of the matrix is returned.
981 const glsl_type
*column_type() const
986 if (explicit_stride
&& interface_row_major
)
987 return get_instance(base_type
, vector_elements
, 1, explicit_stride
);
989 return get_instance(base_type
, vector_elements
, 1);
993 * Get the type of a structure field
996 * Pointer to the type of the named field. If the type is not a structure
997 * or the named field does not exist, \c glsl_type::error_type is returned.
999 const glsl_type
*field_type(const char *name
) const;
1002 * Get the location of a field within a record type
1004 int field_index(const char *name
) const;
1007 * Query the number of elements in an array type
1010 * The number of elements in the array for array types or -1 for non-array
1011 * types. If the number of elements in the array has not yet been declared,
1014 int array_size() const
1016 return is_array() ? length
: -1;
1020 * Query whether the array size for all dimensions has been declared.
1022 bool is_unsized_array() const
1024 return is_array() && length
== 0;
1028 * Return the number of coordinate components needed for this
1029 * sampler or image type.
1031 * This is based purely on the sampler's dimensionality. For example, this
1032 * returns 1 for sampler1D, and 3 for sampler2DArray.
1034 * Note that this is often different than actual coordinate type used in
1035 * a texturing built-in function, since those pack additional values (such
1036 * as the shadow comparator or projector) into the coordinate type.
1038 int coordinate_components() const;
1041 * Compares whether this type matches another type without taking into
1042 * account the precision in structures.
1044 * This is applied recursively so that structures containing structure
1045 * members can also ignore the precision.
1047 bool compare_no_precision(const glsl_type
*b
) const;
1050 * Compare a record type against another record type.
1052 * This is useful for matching record types declared on the same shader
1053 * stage as well as across different shader stages.
1054 * The option to not match name is needed for matching record types
1055 * declared across different shader stages.
1056 * The option to not match locations is to deal with places where the
1057 * same struct is defined in a block which has a location set on it.
1059 bool record_compare(const glsl_type
*b
, bool match_name
,
1060 bool match_locations
= true,
1061 bool match_precision
= true) const;
1064 * Get the type interface packing.
1066 enum glsl_interface_packing
get_interface_packing() const
1068 return (enum glsl_interface_packing
)interface_packing
;
1072 * Get the type interface packing used internally. For shared and packing
1073 * layouts this is implementation defined.
1075 enum glsl_interface_packing
get_internal_ifc_packing(bool std430_supported
) const
1077 enum glsl_interface_packing packing
= this->get_interface_packing();
1078 if (packing
== GLSL_INTERFACE_PACKING_STD140
||
1079 (!std430_supported
&&
1080 (packing
== GLSL_INTERFACE_PACKING_SHARED
||
1081 packing
== GLSL_INTERFACE_PACKING_PACKED
))) {
1082 return GLSL_INTERFACE_PACKING_STD140
;
1084 assert(packing
== GLSL_INTERFACE_PACKING_STD430
||
1085 (std430_supported
&&
1086 (packing
== GLSL_INTERFACE_PACKING_SHARED
||
1087 packing
== GLSL_INTERFACE_PACKING_PACKED
)));
1088 return GLSL_INTERFACE_PACKING_STD430
;
1093 * Check if the type interface is row major
1095 bool get_interface_row_major() const
1097 return (bool) interface_row_major
;
1104 static mtx_t hash_mutex
;
1107 * ralloc context for the type itself.
1111 /** Constructor for vector and matrix types */
1112 glsl_type(GLenum gl_type
,
1113 glsl_base_type base_type
, unsigned vector_elements
,
1114 unsigned matrix_columns
, const char *name
,
1115 unsigned explicit_stride
= 0, bool row_major
= false);
1117 /** Constructor for sampler or image types */
1118 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
1119 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
1120 glsl_base_type type
, const char *name
);
1122 /** Constructor for record types */
1123 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
1124 const char *name
, bool packed
= false);
1126 /** Constructor for interface types */
1127 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
1128 enum glsl_interface_packing packing
,
1129 bool row_major
, const char *name
);
1131 /** Constructor for interface types */
1132 glsl_type(const glsl_type
*return_type
,
1133 const glsl_function_param
*params
, unsigned num_params
);
1135 /** Constructors for array types */
1136 glsl_type(const glsl_type
*array
, unsigned length
, unsigned explicit_stride
);
1138 /** Constructor for subroutine types */
1139 glsl_type(const char *name
);
1141 /** Hash table containing the known explicit matrix and vector types. */
1142 static struct hash_table
*explicit_matrix_types
;
1144 /** Hash table containing the known array types. */
1145 static struct hash_table
*array_types
;
1147 /** Hash table containing the known struct types. */
1148 static struct hash_table
*struct_types
;
1150 /** Hash table containing the known interface types. */
1151 static struct hash_table
*interface_types
;
1153 /** Hash table containing the known subroutine types. */
1154 static struct hash_table
*subroutine_types
;
1156 /** Hash table containing the known function types. */
1157 static struct hash_table
*function_types
;
1159 static bool record_key_compare(const void *a
, const void *b
);
1160 static unsigned record_key_hash(const void *key
);
1163 * \name Built-in type flyweights
1167 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
1169 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
1170 #include "compiler/builtin_type_macros.h"
1174 * \name Friend functions.
1176 * These functions are friends because they must have C linkage and the
1177 * need to call various private methods or access various private static
1181 friend void glsl_type_singleton_init_or_ref(void);
1182 friend void glsl_type_singleton_decref(void);
1183 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
1189 #endif /* __cplusplus */
1191 struct glsl_struct_field
{
1192 const struct glsl_type
*type
;
1196 * For interface blocks, gl_varying_slot corresponding to the input/output
1197 * if this is a built-in input/output (i.e. a member of the built-in
1198 * gl_PerVertex interface block); -1 otherwise.
1200 * Ignored for structs.
1205 * For interface blocks, members may have an explicit byte offset
1206 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
1208 * Unless used for xfb_offset this field is ignored for structs.
1213 * For interface blocks, members may define a transform feedback buffer;
1219 * For interface blocks, members may define a transform feedback stride;
1225 * For interface blocks, the interpolation mode (as in
1226 * ir_variable::interpolation). 0 otherwise.
1228 unsigned interpolation
:2;
1231 * For interface blocks, 1 if this variable uses centroid interpolation (as
1232 * in ir_variable::centroid). 0 otherwise.
1234 unsigned centroid
:1;
1237 * For interface blocks, 1 if this variable uses sample interpolation (as
1238 * in ir_variable::sample). 0 otherwise.
1243 * Layout of the matrix. Uses glsl_matrix_layout values.
1245 unsigned matrix_layout
:2;
1248 * For interface blocks, 1 if this variable is a per-patch input or output
1249 * (as in ir_variable::patch). 0 otherwise.
1254 * Precision qualifier
1256 unsigned precision
:2;
1259 * Memory qualifiers, applicable to buffer variables defined in shader
1260 * storage buffer objects (SSBOs)
1262 unsigned memory_read_only
:1;
1263 unsigned memory_write_only
:1;
1264 unsigned memory_coherent
:1;
1265 unsigned memory_volatile
:1;
1266 unsigned memory_restrict
:1;
1269 * Layout format, applicable to image variables only.
1271 unsigned image_format
:16;
1274 * Any of the xfb_* qualifiers trigger the shader to be in transform
1275 * feedback mode so we need to keep track of whether the buffer was
1276 * explicitly set or if its just been assigned the default global value.
1278 unsigned explicit_xfb_buffer
:1;
1280 unsigned implicit_sized_array
:1;
1282 #define DEFAULT_CONSTRUCTORS(_type, _precision, _name) \
1283 type(_type), name(_name), location(-1), offset(-1), xfb_buffer(0), \
1284 xfb_stride(0), interpolation(0), centroid(0), \
1285 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED), patch(0), \
1286 precision(_precision), memory_read_only(0), \
1287 memory_write_only(0), memory_coherent(0), memory_volatile(0), \
1288 memory_restrict(0), image_format(0), explicit_xfb_buffer(0), \
1289 implicit_sized_array(0)
1291 glsl_struct_field(const struct glsl_type
*_type
,
1294 : DEFAULT_CONSTRUCTORS(_type
, _precision
, _name
)
1299 glsl_struct_field(const struct glsl_type
*_type
, const char *_name
)
1300 : DEFAULT_CONSTRUCTORS(_type
, GLSL_PRECISION_NONE
, _name
)
1306 : DEFAULT_CONSTRUCTORS(NULL
, GLSL_PRECISION_NONE
, NULL
)
1310 #undef DEFAULT_CONSTRUCTORS
1314 struct glsl_function_param
{
1315 const struct glsl_type
*type
;
1321 static inline unsigned int
1322 glsl_align(unsigned int a
, unsigned int align
)
1324 return (a
+ align
- 1) / align
* align
;
1327 #endif /* GLSL_TYPES_H */