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 * Explicit alignment. This is used to communicate explicit alignment
337 * constraints. Should be 0 if the type has no explicit alignment
340 unsigned explicit_alignment
;
343 * Subtype of composite data types.
346 const struct glsl_type
*array
; /**< Type of array elements. */
347 struct glsl_function_param
*parameters
; /**< Parameters to function. */
348 struct glsl_struct_field
*structure
; /**< List of struct fields. */
352 * \name Pointers to various public type singletons
356 #define DECL_TYPE(NAME, ...) \
357 static const glsl_type *const NAME##_type;
359 #define STRUCT_TYPE(NAME) \
360 static const glsl_type *const struct_##NAME##_type;
361 #include "compiler/builtin_type_macros.h"
365 * Convenience accessors for vector types (shorter than get_instance()).
368 static const glsl_type
*vec(unsigned components
, const glsl_type
*const ts
[]);
369 static const glsl_type
*vec(unsigned components
);
370 static const glsl_type
*f16vec(unsigned components
);
371 static const glsl_type
*dvec(unsigned components
);
372 static const glsl_type
*ivec(unsigned components
);
373 static const glsl_type
*uvec(unsigned components
);
374 static const glsl_type
*bvec(unsigned components
);
375 static const glsl_type
*i64vec(unsigned components
);
376 static const glsl_type
*u64vec(unsigned components
);
377 static const glsl_type
*i16vec(unsigned components
);
378 static const glsl_type
*u16vec(unsigned components
);
379 static const glsl_type
*i8vec(unsigned components
);
380 static const glsl_type
*u8vec(unsigned components
);
384 * For numeric and boolean derived types returns the basic scalar type
386 * If the type is a numeric or boolean scalar, vector, or matrix type,
387 * this function gets the scalar type of the individual components. For
388 * all other types, including arrays of numeric or boolean types, the
389 * error type is returned.
391 const glsl_type
*get_base_type() const;
394 * Get the basic scalar type which this type aggregates.
396 * If the type is a numeric or boolean scalar, vector, or matrix, or an
397 * array of any of those, this function gets the scalar type of the
398 * individual components. For structs and arrays of structs, this function
399 * returns the struct type. For samplers and arrays of samplers, this
400 * function returns the sampler type.
402 const glsl_type
*get_scalar_type() const;
405 * Gets the "bare" type without any decorations or layout information.
407 const glsl_type
*get_bare_type() const;
410 * Gets the float16 version of this type.
412 const glsl_type
*get_float16_type() const;
415 * Gets the int16 version of this type.
417 const glsl_type
*get_int16_type() const;
420 * Gets the uint16 version of this type.
422 const glsl_type
*get_uint16_type() const;
425 * Get the instance of a built-in scalar, vector, or matrix type
427 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
429 unsigned explicit_stride
= 0,
430 bool row_major
= false,
431 unsigned explicit_alignment
= 0);
434 * Get the instance of a sampler type
436 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
439 glsl_base_type type
);
441 static const glsl_type
*get_image_instance(enum glsl_sampler_dim dim
,
442 bool array
, glsl_base_type type
);
445 * Get the instance of an array type
447 static const glsl_type
*get_array_instance(const glsl_type
*base
,
449 unsigned explicit_stride
= 0);
452 * Get the instance of a record type
454 static const glsl_type
*get_struct_instance(const glsl_struct_field
*fields
,
458 unsigned explicit_alignment
= 0);
461 * Get the instance of an interface block type
463 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
465 enum glsl_interface_packing packing
,
467 const char *block_name
);
470 * Get the instance of an subroutine type
472 static const glsl_type
*get_subroutine_instance(const char *subroutine_name
);
475 * Get the instance of a function type
477 static const glsl_type
*get_function_instance(const struct glsl_type
*return_type
,
478 const glsl_function_param
*parameters
,
479 unsigned num_params
);
482 * Get the type resulting from a multiplication of \p type_a * \p type_b
484 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
485 const glsl_type
*type_b
);
488 * Query the total number of scalars that make up a scalar, vector or matrix
490 unsigned components() const
492 return vector_elements
* matrix_columns
;
496 * Calculate the number of components slots required to hold this type
498 * This is used to determine how many uniform or varying locations a type
501 unsigned component_slots() const;
504 * Calculate offset between the base location of the struct in
505 * uniform storage and a struct member.
506 * For the initial call, length is the index of the member to find the
509 unsigned struct_location_offset(unsigned length
) const;
512 * Calculate the number of unique values from glGetUniformLocation for the
513 * elements of the type.
515 * This is used to allocate slots in the UniformRemapTable, the amount of
516 * locations may not match with actual used storage space by the driver.
518 unsigned uniform_locations() const;
521 * Used to count the number of varyings contained in the type ignoring
522 * innermost array elements.
524 unsigned varying_count() const;
527 * Calculate the number of vec4 slots required to hold this type.
529 * This is the underlying recursive type_size function for
530 * count_attribute_slots() (vertex inputs and varyings) but also for
531 * gallium's !PIPE_CAP_PACKED_UNIFORMS case.
533 unsigned count_vec4_slots(bool is_gl_vertex_input
, bool bindless
) const;
536 * Calculate the number of vec4 slots required to hold this type.
538 * This is the underlying recursive type_size function for
539 * gallium's PIPE_CAP_PACKED_UNIFORMS case.
541 unsigned count_dword_slots(bool bindless
) const;
544 * Calculate the number of attribute slots required to hold this type
546 * This implements the language rules of GLSL 1.50 for counting the number
547 * of slots used by a vertex attribute. It also determines the number of
548 * varying slots the type will use up in the absence of varying packing
549 * (and thus, it can be used to measure the number of varying slots used by
550 * the varyings that are generated by lower_packed_varyings).
552 * For vertex shader attributes - doubles only take one slot.
553 * For inter-shader varyings - dvec3/dvec4 take two slots.
555 * Vulkan doesn’t make this distinction so the argument should always be
558 unsigned count_attribute_slots(bool is_gl_vertex_input
) const {
559 return count_vec4_slots(is_gl_vertex_input
, true);
563 * Alignment in bytes of the start of this type in a std140 uniform
566 unsigned std140_base_alignment(bool row_major
) const;
568 /** Size in bytes of this type in a std140 uniform block.
570 * Note that this is not GL_UNIFORM_SIZE (which is the number of
571 * elements in the array)
573 unsigned std140_size(bool row_major
) const;
576 * Gets an explicitly laid out type with the std140 layout.
578 const glsl_type
*get_explicit_std140_type(bool row_major
) const;
581 * Alignment in bytes of the start of this type in a std430 shader
584 unsigned std430_base_alignment(bool row_major
) const;
587 * Calculate array stride in bytes of this type in a std430 shader storage
590 unsigned std430_array_stride(bool row_major
) const;
593 * Size in bytes of this type in a std430 shader storage block.
595 * Note that this is not GL_BUFFER_SIZE
597 unsigned std430_size(bool row_major
) const;
600 * Gets an explicitly laid out type with the std430 layout.
602 const glsl_type
*get_explicit_std430_type(bool row_major
) const;
605 * Gets an explicitly laid out interface type.
607 const glsl_type
*get_explicit_interface_type(bool supports_std430
) const;
609 /** Returns an explicitly laid out type given a type and size/align func
611 * The size/align func is only called for scalar and vector types and the
612 * returned type is otherwise laid out in the natural way as follows:
614 * - Arrays and matrices have a stride of ALIGN(elem_size, elem_align).
616 * - Structure types have their elements in-order and as tightly packed as
617 * possible following the alignment required by the size/align func.
619 * - All composite types (structures, matrices, and arrays) have an
620 * alignment equal to the highest alighment of any member of the composite.
622 * The types returned by this function are likely not suitable for most UBO
623 * or SSBO layout because they do not add the extra array and substructure
624 * alignment that is required by std140 and std430.
626 const glsl_type
*get_explicit_type_for_size_align(glsl_type_size_align_func type_info
,
627 unsigned *size
, unsigned *align
) const;
630 * Alignment in bytes of the start of this type in OpenCL memory.
632 unsigned cl_alignment() const;
635 * Size in bytes of this type in OpenCL memory
637 unsigned cl_size() const;
640 * Size in bytes of this type based on its explicit data.
642 * When using SPIR-V shaders (ARB_gl_spirv), memory layouts are expressed
643 * through explicit offset, stride and matrix layout, so the size
644 * can/should be computed used those values.
646 * Note that the value returned by this method is only correct if such
647 * values are set, so only with SPIR-V shaders. Should not be used with
650 unsigned explicit_size(bool align_to_stride
=false) const;
653 * \brief Can this type be implicitly converted to another?
655 * \return True if the types are identical or if this type can be converted
656 * to \c desired according to Section 4.1.10 of the GLSL spec.
659 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
660 * Implicit Conversions:
662 * In some situations, an expression and its type will be implicitly
663 * converted to a different type. The following table shows all allowed
664 * implicit conversions:
666 * Type of expression | Can be implicitly converted to
667 * --------------------------------------------------
680 * There are no implicit array or structure conversions. For example,
681 * an array of int cannot be implicitly converted to an array of float.
682 * There are no implicit conversions between signed and unsigned
686 bool can_implicitly_convert_to(const glsl_type
*desired
,
687 _mesa_glsl_parse_state
*state
) const;
690 * Query whether or not a type is a scalar (non-vector and non-matrix).
692 bool is_scalar() const
694 return (vector_elements
== 1)
695 && (base_type
>= GLSL_TYPE_UINT
)
696 && (base_type
<= GLSL_TYPE_IMAGE
);
700 * Query whether or not a type is a vector
702 bool is_vector() const
704 return (vector_elements
> 1)
705 && (matrix_columns
== 1)
706 && (base_type
>= GLSL_TYPE_UINT
)
707 && (base_type
<= GLSL_TYPE_BOOL
);
711 * Query whether or not a type is a matrix
713 bool is_matrix() const
715 /* GLSL only has float matrices. */
716 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
||
717 base_type
== GLSL_TYPE_DOUBLE
||
718 base_type
== GLSL_TYPE_FLOAT16
);
722 * Query whether or not a type is a non-array numeric type
724 bool is_numeric() const
726 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_INT64
);
730 * Query whether or not a type is an integer.
732 bool is_integer() const
734 return glsl_base_type_is_integer(base_type
);
738 * Query whether or not a type is a 16-bit integer.
740 bool is_integer_16() const
742 return base_type
== GLSL_TYPE_UINT16
|| base_type
== GLSL_TYPE_INT16
;
746 * Query whether or not a type is an 32-bit integer.
748 bool is_integer_32() const
750 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
754 * Query whether or not a type is a 64-bit integer.
756 bool is_integer_64() const
758 return base_type
== GLSL_TYPE_UINT64
|| base_type
== GLSL_TYPE_INT64
;
762 * Query whether or not a type is a 32-bit or 64-bit integer
764 bool is_integer_32_64() const
766 return is_integer_32() || is_integer_64();
770 * Query whether or not a type is a 16-bit or 32-bit integer
772 bool is_integer_16_32() const
774 return is_integer_16() || is_integer_32() || is_integer_64();
778 * Query whether or not a type is a 16-bit, 32-bit or 64-bit integer
780 bool is_integer_16_32_64() const
782 return is_integer_16() || is_integer_32() || is_integer_64();
786 * Query whether or not type is an integral type, or for struct and array
787 * types, contains an integral type.
789 bool contains_integer() const;
792 * Query whether or not type is a double type, or for struct, interface and
793 * array types, contains a double type.
795 bool contains_double() const;
798 * Query whether or not type is a 64-bit type, or for struct, interface and
799 * array types, contains a double type.
801 bool contains_64bit() const;
804 * Query whether or not a type is a float type
806 bool is_float() const
808 return base_type
== GLSL_TYPE_FLOAT
;
812 * Query whether or not a type is a half-float or float type
814 bool is_float_16_32() const
816 return base_type
== GLSL_TYPE_FLOAT16
|| is_float();
820 * Query whether or not a type is a half-float, float or double
822 bool is_float_16_32_64() const
824 return base_type
== GLSL_TYPE_FLOAT16
|| is_float() || is_double();
828 * Query whether or not a type is a float or double
830 bool is_float_32_64() const
832 return is_float() || is_double();
835 bool is_int_16_32_64() const
837 return base_type
== GLSL_TYPE_INT16
||
838 base_type
== GLSL_TYPE_INT
||
839 base_type
== GLSL_TYPE_INT64
;
842 bool is_uint_16_32_64() const
844 return base_type
== GLSL_TYPE_UINT16
||
845 base_type
== GLSL_TYPE_UINT
||
846 base_type
== GLSL_TYPE_UINT64
;
849 bool is_int_16_32() const
851 return base_type
== GLSL_TYPE_INT
||
852 base_type
== GLSL_TYPE_INT16
;
855 bool is_uint_16_32() const
857 return base_type
== GLSL_TYPE_UINT
||
858 base_type
== GLSL_TYPE_UINT16
;
862 * Query whether or not a type is a double type
864 bool is_double() const
866 return base_type
== GLSL_TYPE_DOUBLE
;
870 * Query whether a 64-bit type takes two slots.
872 bool is_dual_slot() const
874 return is_64bit() && vector_elements
> 2;
878 * Query whether or not a type is 64-bit
880 bool is_64bit() const
882 return glsl_base_type_is_64bit(base_type
);
886 * Query whether or not a type is 16-bit
888 bool is_16bit() const
890 return glsl_base_type_is_16bit(base_type
);
894 * Query whether or not a type is 32-bit
896 bool is_32bit() const
898 return base_type
== GLSL_TYPE_UINT
||
899 base_type
== GLSL_TYPE_INT
||
900 base_type
== GLSL_TYPE_FLOAT
;
904 * Query whether or not a type is a non-array boolean type
906 bool is_boolean() const
908 return base_type
== GLSL_TYPE_BOOL
;
912 * Query whether or not a type is a sampler
914 bool is_sampler() const
916 return base_type
== GLSL_TYPE_SAMPLER
;
920 * Query whether or not type is a sampler, or for struct, interface and
921 * array types, contains a sampler.
923 bool contains_sampler() const;
926 * Query whether or not type is an array or for struct, interface and
927 * array types, contains an array.
929 bool contains_array() const;
932 * Get the Mesa texture target index for a sampler type.
934 gl_texture_index
sampler_index() const;
937 * Query whether or not type is an image, or for struct, interface and
938 * array types, contains an image.
940 bool contains_image() const;
943 * Query whether or not a type is an image
945 bool is_image() const
947 return base_type
== GLSL_TYPE_IMAGE
;
951 * Query whether or not a type is an array
953 bool is_array() const
955 return base_type
== GLSL_TYPE_ARRAY
;
958 bool is_array_of_arrays() const
960 return is_array() && fields
.array
->is_array();
964 * Query whether or not a type is a record
966 bool is_struct() const
968 return base_type
== GLSL_TYPE_STRUCT
;
972 * Query whether or not a type is an interface
974 bool is_interface() const
976 return base_type
== GLSL_TYPE_INTERFACE
;
980 * Query whether or not a type is the void type singleton.
984 return base_type
== GLSL_TYPE_VOID
;
988 * Query whether or not a type is the error type singleton.
990 bool is_error() const
992 return base_type
== GLSL_TYPE_ERROR
;
996 * Query if a type is unnamed/anonymous (named by the parser)
999 bool is_subroutine() const
1001 return base_type
== GLSL_TYPE_SUBROUTINE
;
1003 bool contains_subroutine() const;
1005 bool is_anonymous() const
1007 return !strncmp(name
, "#anon", 5);
1011 * Get the type stripped of any arrays
1014 * Pointer to the type of elements of the first non-array type for array
1015 * types, or pointer to itself for non-array types.
1017 const glsl_type
*without_array() const
1019 const glsl_type
*t
= this;
1021 while (t
->is_array())
1022 t
= t
->fields
.array
;
1028 * Return the total number of elements in an array including the elements
1029 * in arrays of arrays.
1031 unsigned arrays_of_arrays_size() const
1036 unsigned size
= length
;
1037 const glsl_type
*base_type
= fields
.array
;
1039 while (base_type
->is_array()) {
1040 size
= size
* base_type
->length
;
1041 base_type
= base_type
->fields
.array
;
1047 * Return bit size for this type.
1049 unsigned bit_size() const
1051 return glsl_base_type_bit_size(this->base_type
);
1056 * Query whether or not a type is an atomic_uint.
1058 bool is_atomic_uint() const
1060 return base_type
== GLSL_TYPE_ATOMIC_UINT
;
1064 * Return the amount of atomic counter storage required for a type.
1066 unsigned atomic_size() const
1068 if (is_atomic_uint())
1069 return ATOMIC_COUNTER_SIZE
;
1070 else if (is_array())
1071 return length
* fields
.array
->atomic_size();
1077 * Return whether a type contains any atomic counters.
1079 bool contains_atomic() const
1081 return atomic_size() > 0;
1085 * Return whether a type contains any opaque types.
1087 bool contains_opaque() const;
1090 * Query the full type of a matrix row
1093 * If the type is not a matrix, \c glsl_type::error_type is returned.
1094 * Otherwise a type matching the rows of the matrix is returned.
1096 const glsl_type
*row_type() const
1101 if (explicit_stride
&& !interface_row_major
)
1102 return get_instance(base_type
, matrix_columns
, 1, explicit_stride
);
1104 return get_instance(base_type
, matrix_columns
, 1);
1108 * Query the full type of a matrix column
1111 * If the type is not a matrix, \c glsl_type::error_type is returned.
1112 * Otherwise a type matching the columns of the matrix is returned.
1114 const glsl_type
*column_type() const
1119 if (interface_row_major
) {
1120 /* If we're row-major, the vector element stride is the same as the
1121 * matrix stride and we have no alignment (i.e. component-aligned).
1123 return get_instance(base_type
, vector_elements
, 1,
1124 explicit_stride
, false, 0);
1126 /* Otherwise, the vector is tightly packed (stride=0). For
1127 * alignment, we treat a matrix as an array of columns make the same
1128 * assumption that the alignment of the column is the same as the
1129 * alignment of the whole matrix.
1131 return get_instance(base_type
, vector_elements
, 1,
1132 0, false, explicit_alignment
);
1137 * Get the type of a structure field
1140 * Pointer to the type of the named field. If the type is not a structure
1141 * or the named field does not exist, \c glsl_type::error_type is returned.
1143 const glsl_type
*field_type(const char *name
) const;
1146 * Get the location of a field within a record type
1148 int field_index(const char *name
) const;
1151 * Query the number of elements in an array type
1154 * The number of elements in the array for array types or -1 for non-array
1155 * types. If the number of elements in the array has not yet been declared,
1158 int array_size() const
1160 return is_array() ? length
: -1;
1164 * Query whether the array size for all dimensions has been declared.
1166 bool is_unsized_array() const
1168 return is_array() && length
== 0;
1172 * Return the number of coordinate components needed for this
1173 * sampler or image type.
1175 * This is based purely on the sampler's dimensionality. For example, this
1176 * returns 1 for sampler1D, and 3 for sampler2DArray.
1178 * Note that this is often different than actual coordinate type used in
1179 * a texturing built-in function, since those pack additional values (such
1180 * as the shadow comparator or projector) into the coordinate type.
1182 int coordinate_components() const;
1185 * Compares whether this type matches another type without taking into
1186 * account the precision in structures.
1188 * This is applied recursively so that structures containing structure
1189 * members can also ignore the precision.
1191 bool compare_no_precision(const glsl_type
*b
) const;
1194 * Compare a record type against another record type.
1196 * This is useful for matching record types declared on the same shader
1197 * stage as well as across different shader stages.
1198 * The option to not match name is needed for matching record types
1199 * declared across different shader stages.
1200 * The option to not match locations is to deal with places where the
1201 * same struct is defined in a block which has a location set on it.
1203 bool record_compare(const glsl_type
*b
, bool match_name
,
1204 bool match_locations
= true,
1205 bool match_precision
= true) const;
1208 * Get the type interface packing.
1210 enum glsl_interface_packing
get_interface_packing() const
1212 return (enum glsl_interface_packing
)interface_packing
;
1216 * Get the type interface packing used internally. For shared and packing
1217 * layouts this is implementation defined.
1219 enum glsl_interface_packing
get_internal_ifc_packing(bool std430_supported
) const
1221 enum glsl_interface_packing packing
= this->get_interface_packing();
1222 if (packing
== GLSL_INTERFACE_PACKING_STD140
||
1223 (!std430_supported
&&
1224 (packing
== GLSL_INTERFACE_PACKING_SHARED
||
1225 packing
== GLSL_INTERFACE_PACKING_PACKED
))) {
1226 return GLSL_INTERFACE_PACKING_STD140
;
1228 assert(packing
== GLSL_INTERFACE_PACKING_STD430
||
1229 (std430_supported
&&
1230 (packing
== GLSL_INTERFACE_PACKING_SHARED
||
1231 packing
== GLSL_INTERFACE_PACKING_PACKED
)));
1232 return GLSL_INTERFACE_PACKING_STD430
;
1237 * Check if the type interface is row major
1239 bool get_interface_row_major() const
1241 return (bool) interface_row_major
;
1248 static mtx_t hash_mutex
;
1251 * ralloc context for the type itself.
1255 /** Constructor for vector and matrix types */
1256 glsl_type(GLenum gl_type
,
1257 glsl_base_type base_type
, unsigned vector_elements
,
1258 unsigned matrix_columns
, const char *name
,
1259 unsigned explicit_stride
= 0, bool row_major
= false,
1260 unsigned explicit_alignment
= 0);
1262 /** Constructor for sampler or image types */
1263 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
1264 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
1265 glsl_base_type type
, const char *name
);
1267 /** Constructor for record types */
1268 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
1269 const char *name
, bool packed
= false,
1270 unsigned explicit_alignment
= 0);
1272 /** Constructor for interface types */
1273 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
1274 enum glsl_interface_packing packing
,
1275 bool row_major
, const char *name
);
1277 /** Constructor for interface types */
1278 glsl_type(const glsl_type
*return_type
,
1279 const glsl_function_param
*params
, unsigned num_params
);
1281 /** Constructors for array types */
1282 glsl_type(const glsl_type
*array
, unsigned length
, unsigned explicit_stride
);
1284 /** Constructor for subroutine types */
1285 glsl_type(const char *name
);
1287 /** Hash table containing the known explicit matrix and vector types. */
1288 static struct hash_table
*explicit_matrix_types
;
1290 /** Hash table containing the known array types. */
1291 static struct hash_table
*array_types
;
1293 /** Hash table containing the known struct types. */
1294 static struct hash_table
*struct_types
;
1296 /** Hash table containing the known interface types. */
1297 static struct hash_table
*interface_types
;
1299 /** Hash table containing the known subroutine types. */
1300 static struct hash_table
*subroutine_types
;
1302 /** Hash table containing the known function types. */
1303 static struct hash_table
*function_types
;
1305 static bool record_key_compare(const void *a
, const void *b
);
1306 static unsigned record_key_hash(const void *key
);
1309 * \name Built-in type flyweights
1313 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
1315 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
1316 #include "compiler/builtin_type_macros.h"
1320 * \name Friend functions.
1322 * These functions are friends because they must have C linkage and the
1323 * need to call various private methods or access various private static
1327 friend void glsl_type_singleton_init_or_ref(void);
1328 friend void glsl_type_singleton_decref(void);
1329 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
1335 #endif /* __cplusplus */
1337 struct glsl_struct_field
{
1338 const struct glsl_type
*type
;
1342 * For interface blocks, gl_varying_slot corresponding to the input/output
1343 * if this is a built-in input/output (i.e. a member of the built-in
1344 * gl_PerVertex interface block); -1 otherwise.
1346 * Ignored for structs.
1351 * For interface blocks, members may have an explicit byte offset
1352 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
1354 * Unless used for xfb_offset this field is ignored for structs.
1359 * For interface blocks, members may define a transform feedback buffer;
1365 * For interface blocks, members may define a transform feedback stride;
1370 * Layout format, applicable to image variables only.
1372 enum pipe_format image_format
;
1377 * For interface blocks, the interpolation mode (as in
1378 * ir_variable::interpolation). 0 otherwise.
1380 unsigned interpolation
:3;
1383 * For interface blocks, 1 if this variable uses centroid interpolation (as
1384 * in ir_variable::centroid). 0 otherwise.
1386 unsigned centroid
:1;
1389 * For interface blocks, 1 if this variable uses sample interpolation (as
1390 * in ir_variable::sample). 0 otherwise.
1395 * Layout of the matrix. Uses glsl_matrix_layout values.
1397 unsigned matrix_layout
:2;
1400 * For interface blocks, 1 if this variable is a per-patch input or output
1401 * (as in ir_variable::patch). 0 otherwise.
1406 * Precision qualifier
1408 unsigned precision
:2;
1411 * Memory qualifiers, applicable to buffer variables defined in shader
1412 * storage buffer objects (SSBOs)
1414 unsigned memory_read_only
:1;
1415 unsigned memory_write_only
:1;
1416 unsigned memory_coherent
:1;
1417 unsigned memory_volatile
:1;
1418 unsigned memory_restrict
:1;
1421 * Any of the xfb_* qualifiers trigger the shader to be in transform
1422 * feedback mode so we need to keep track of whether the buffer was
1423 * explicitly set or if its just been assigned the default global value.
1425 unsigned explicit_xfb_buffer
:1;
1427 unsigned implicit_sized_array
:1;
1432 #define DEFAULT_CONSTRUCTORS(_type, _name) \
1433 type(_type), name(_name), location(-1), offset(-1), xfb_buffer(0), \
1434 xfb_stride(0), image_format(PIPE_FORMAT_NONE), flags(0) \
1436 glsl_struct_field(const struct glsl_type *_type,
1439 : DEFAULT_CONSTRUCTORS(_type
, _name
)
1441 matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
1442 precision
= _precision
;
1445 glsl_struct_field(const struct glsl_type
*_type
, const char *_name
)
1446 : DEFAULT_CONSTRUCTORS(_type
, _name
)
1448 matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
1449 precision
= GLSL_PRECISION_NONE
;
1453 : DEFAULT_CONSTRUCTORS(NULL
, NULL
)
1455 matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
1456 precision
= GLSL_PRECISION_NONE
;
1458 #undef DEFAULT_CONSTRUCTORS
1462 struct glsl_function_param
{
1463 const struct glsl_type
*type
;
1469 static inline unsigned int
1470 glsl_align(unsigned int a
, unsigned int align
)
1472 return (a
+ align
- 1) / align
* align
;
1475 #endif /* GLSL_TYPES_H */