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
);
63 typedef void (*glsl_type_size_align_func
)(const struct glsl_type
*type
,
64 unsigned *size
, unsigned *align
);
67 /* Note: GLSL_TYPE_UINT, GLSL_TYPE_INT, and GLSL_TYPE_FLOAT must be 0, 1,
68 * and 2 so that they will fit in the 2 bits of glsl_type::sampled_type.
84 GLSL_TYPE_ATOMIC_UINT
,
94 /* Return the bit size of a type. Note that this differs from
95 * glsl_get_bit_size in that it returns 32 bits for bools, whereas at
96 * the NIR level we would want to return 1 bit for bools.
98 static unsigned glsl_base_type_bit_size(enum glsl_base_type type
)
104 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
105 case GLSL_TYPE_SUBROUTINE
:
108 case GLSL_TYPE_FLOAT16
:
109 case GLSL_TYPE_UINT16
:
110 case GLSL_TYPE_INT16
:
113 case GLSL_TYPE_UINT8
:
117 case GLSL_TYPE_DOUBLE
:
118 case GLSL_TYPE_INT64
:
119 case GLSL_TYPE_UINT64
:
120 case GLSL_TYPE_IMAGE
:
121 case GLSL_TYPE_SAMPLER
:
125 /* For GLSL_TYPE_STRUCT etc, it should be ok to return 0. This usually
126 * happens when calling this method through is_64bit and is_16bit
135 static inline bool glsl_base_type_is_16bit(enum glsl_base_type type
)
137 return glsl_base_type_bit_size(type
) == 16;
140 static inline bool glsl_base_type_is_64bit(enum glsl_base_type type
)
142 return glsl_base_type_bit_size(type
) == 64;
145 static inline bool glsl_base_type_is_integer(enum glsl_base_type type
)
147 return type
== GLSL_TYPE_UINT8
||
148 type
== GLSL_TYPE_INT8
||
149 type
== GLSL_TYPE_UINT16
||
150 type
== GLSL_TYPE_INT16
||
151 type
== GLSL_TYPE_UINT
||
152 type
== GLSL_TYPE_INT
||
153 type
== GLSL_TYPE_UINT64
||
154 type
== GLSL_TYPE_INT64
||
155 type
== GLSL_TYPE_BOOL
||
156 type
== GLSL_TYPE_SAMPLER
||
157 type
== GLSL_TYPE_IMAGE
;
160 static inline unsigned int
161 glsl_base_type_get_bit_size(const enum glsl_base_type base_type
)
169 case GLSL_TYPE_FLOAT
: /* TODO handle mediump */
170 case GLSL_TYPE_SUBROUTINE
:
173 case GLSL_TYPE_FLOAT16
:
174 case GLSL_TYPE_UINT16
:
175 case GLSL_TYPE_INT16
:
178 case GLSL_TYPE_UINT8
:
182 case GLSL_TYPE_DOUBLE
:
183 case GLSL_TYPE_INT64
:
184 case GLSL_TYPE_UINT64
:
185 case GLSL_TYPE_IMAGE
:
186 case GLSL_TYPE_SAMPLER
:
190 unreachable("unknown base type");
196 static inline enum glsl_base_type
197 glsl_unsigned_base_type_of(enum glsl_base_type type
)
201 return GLSL_TYPE_UINT
;
203 return GLSL_TYPE_UINT8
;
204 case GLSL_TYPE_INT16
:
205 return GLSL_TYPE_UINT16
;
206 case GLSL_TYPE_INT64
:
207 return GLSL_TYPE_UINT64
;
209 assert(type
== GLSL_TYPE_UINT
||
210 type
== GLSL_TYPE_UINT8
||
211 type
== GLSL_TYPE_UINT16
||
212 type
== GLSL_TYPE_UINT64
);
217 enum glsl_sampler_dim
{
218 GLSL_SAMPLER_DIM_1D
= 0,
221 GLSL_SAMPLER_DIM_CUBE
,
222 GLSL_SAMPLER_DIM_RECT
,
223 GLSL_SAMPLER_DIM_BUF
,
224 GLSL_SAMPLER_DIM_EXTERNAL
,
226 GLSL_SAMPLER_DIM_SUBPASS
, /* for vulkan input attachments */
227 GLSL_SAMPLER_DIM_SUBPASS_MS
, /* for multisampled vulkan input attachments */
231 glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim
);
233 enum glsl_matrix_layout
{
235 * The layout of the matrix is inherited from the object containing the
236 * matrix (the top level structure or the uniform block).
238 GLSL_MATRIX_LAYOUT_INHERITED
,
241 * Explicit column-major layout
243 * If a uniform block doesn't have an explicit layout set, it will default
246 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
,
251 GLSL_MATRIX_LAYOUT_ROW_MAJOR
255 GLSL_PRECISION_NONE
= 0,
257 GLSL_PRECISION_MEDIUM
,
265 #include "util/ralloc.h"
266 #include "main/menums.h" /* for gl_texture_index, C++'s enum rules are broken */
270 glsl_base_type base_type
:8;
272 glsl_base_type sampled_type
:8; /**< Type of data returned using this
273 * sampler or image. Only \c
274 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
275 * and \c GLSL_TYPE_UINT are valid.
278 unsigned sampler_dimensionality
:4; /**< \see glsl_sampler_dim */
279 unsigned sampler_shadow
:1;
280 unsigned sampler_array
:1;
281 unsigned interface_packing
:2;
282 unsigned interface_row_major
:1;
285 * For \c GLSL_TYPE_STRUCT this specifies if the struct is packed or not.
287 * Only used for Compute kernels
292 glsl_type() : mem_ctx(NULL
)
294 // Dummy constructor, just for the sake of ASSERT_BITFIELD_SIZE.
299 * \name Vector and matrix element counts
301 * For scalars, each of these values will be 1. For non-numeric types
305 uint8_t vector_elements
; /**< 1, 2, 3, or 4 vector elements. */
306 uint8_t matrix_columns
; /**< 1, 2, 3, or 4 matrix columns. */
310 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
311 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
312 * elements in the structure and the number of values pointed to by
313 * \c fields.structure (below).
318 * Name of the data type
320 * Will never be \c NULL.
325 * Explicit array, matrix, or vector stride. This is used to communicate
326 * explicit array layouts from SPIR-V. Should be 0 if the type has no
329 unsigned explicit_stride
;
332 * Subtype of composite data types.
335 const struct glsl_type
*array
; /**< Type of array elements. */
336 struct glsl_function_param
*parameters
; /**< Parameters to function. */
337 struct glsl_struct_field
*structure
; /**< List of struct fields. */
341 * \name Pointers to various public type singletons
345 #define DECL_TYPE(NAME, ...) \
346 static const glsl_type *const NAME##_type;
348 #define STRUCT_TYPE(NAME) \
349 static const glsl_type *const struct_##NAME##_type;
350 #include "compiler/builtin_type_macros.h"
354 * Convenience accessors for vector types (shorter than get_instance()).
357 static const glsl_type
*vec(unsigned components
, const glsl_type
*const ts
[]);
358 static const glsl_type
*vec(unsigned components
);
359 static const glsl_type
*f16vec(unsigned components
);
360 static const glsl_type
*dvec(unsigned components
);
361 static const glsl_type
*ivec(unsigned components
);
362 static const glsl_type
*uvec(unsigned components
);
363 static const glsl_type
*bvec(unsigned components
);
364 static const glsl_type
*i64vec(unsigned components
);
365 static const glsl_type
*u64vec(unsigned components
);
366 static const glsl_type
*i16vec(unsigned components
);
367 static const glsl_type
*u16vec(unsigned components
);
368 static const glsl_type
*i8vec(unsigned components
);
369 static const glsl_type
*u8vec(unsigned components
);
373 * For numeric and boolean derived types returns the basic scalar type
375 * If the type is a numeric or boolean scalar, vector, or matrix type,
376 * this function gets the scalar type of the individual components. For
377 * all other types, including arrays of numeric or boolean types, the
378 * error type is returned.
380 const glsl_type
*get_base_type() const;
383 * Get the basic scalar type which this type aggregates.
385 * If the type is a numeric or boolean scalar, vector, or matrix, or an
386 * array of any of those, this function gets the scalar type of the
387 * individual components. For structs and arrays of structs, this function
388 * returns the struct type. For samplers and arrays of samplers, this
389 * function returns the sampler type.
391 const glsl_type
*get_scalar_type() const;
394 * Gets the "bare" type without any decorations or layout information.
396 const glsl_type
*get_bare_type() const;
399 * Gets the float16 version of this type.
401 const glsl_type
*get_float16_type() const;
404 * Get the instance of a built-in scalar, vector, or matrix type
406 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
408 unsigned explicit_stride
= 0,
409 bool row_major
= false);
412 * Get the instance of a sampler type
414 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
417 glsl_base_type type
);
419 static const glsl_type
*get_image_instance(enum glsl_sampler_dim dim
,
420 bool array
, glsl_base_type type
);
423 * Get the instance of an array type
425 static const glsl_type
*get_array_instance(const glsl_type
*base
,
427 unsigned explicit_stride
= 0);
430 * Get the instance of a record type
432 static const glsl_type
*get_struct_instance(const glsl_struct_field
*fields
,
435 bool packed
= false);
438 * Get the instance of an interface block type
440 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
442 enum glsl_interface_packing packing
,
444 const char *block_name
);
447 * Get the instance of an subroutine type
449 static const glsl_type
*get_subroutine_instance(const char *subroutine_name
);
452 * Get the instance of a function type
454 static const glsl_type
*get_function_instance(const struct glsl_type
*return_type
,
455 const glsl_function_param
*parameters
,
456 unsigned num_params
);
459 * Get the type resulting from a multiplication of \p type_a * \p type_b
461 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
462 const glsl_type
*type_b
);
465 * Query the total number of scalars that make up a scalar, vector or matrix
467 unsigned components() const
469 return vector_elements
* matrix_columns
;
473 * Calculate the number of components slots required to hold this type
475 * This is used to determine how many uniform or varying locations a type
478 unsigned component_slots() const;
481 * Calculate offset between the base location of the struct in
482 * uniform storage and a struct member.
483 * For the initial call, length is the index of the member to find the
486 unsigned struct_location_offset(unsigned length
) const;
489 * Calculate the number of unique values from glGetUniformLocation for the
490 * elements of the type.
492 * This is used to allocate slots in the UniformRemapTable, the amount of
493 * locations may not match with actual used storage space by the driver.
495 unsigned uniform_locations() const;
498 * Used to count the number of varyings contained in the type ignoring
499 * innermost array elements.
501 unsigned varying_count() const;
504 * Calculate the number of vec4 slots required to hold this type.
506 * This is the underlying recursive type_size function for
507 * count_attribute_slots() (vertex inputs and varyings) but also for
508 * gallium's !PIPE_CAP_PACKED_UNIFORMS case.
510 unsigned count_vec4_slots(bool is_gl_vertex_input
, bool bindless
) const;
513 * Calculate the number of vec4 slots required to hold this type.
515 * This is the underlying recursive type_size function for
516 * gallium's PIPE_CAP_PACKED_UNIFORMS case.
518 unsigned count_dword_slots(bool bindless
) const;
521 * Calculate the number of attribute slots required to hold this type
523 * This implements the language rules of GLSL 1.50 for counting the number
524 * of slots used by a vertex attribute. It also determines the number of
525 * varying slots the type will use up in the absence of varying packing
526 * (and thus, it can be used to measure the number of varying slots used by
527 * the varyings that are generated by lower_packed_varyings).
529 * For vertex shader attributes - doubles only take one slot.
530 * For inter-shader varyings - dvec3/dvec4 take two slots.
532 * Vulkan doesn’t make this distinction so the argument should always be
535 unsigned count_attribute_slots(bool is_gl_vertex_input
) const {
536 return count_vec4_slots(is_gl_vertex_input
, true);
540 * Alignment in bytes of the start of this type in a std140 uniform
543 unsigned std140_base_alignment(bool row_major
) const;
545 /** Size in bytes of this type in a std140 uniform block.
547 * Note that this is not GL_UNIFORM_SIZE (which is the number of
548 * elements in the array)
550 unsigned std140_size(bool row_major
) const;
553 * Gets an explicitly laid out type with the std140 layout.
555 const glsl_type
*get_explicit_std140_type(bool row_major
) const;
558 * Alignment in bytes of the start of this type in a std430 shader
561 unsigned std430_base_alignment(bool row_major
) const;
564 * Calculate array stride in bytes of this type in a std430 shader storage
567 unsigned std430_array_stride(bool row_major
) const;
570 * Size in bytes of this type in a std430 shader storage block.
572 * Note that this is not GL_BUFFER_SIZE
574 unsigned std430_size(bool row_major
) const;
577 * Gets an explicitly laid out type with the std430 layout.
579 const glsl_type
*get_explicit_std430_type(bool row_major
) const;
582 * Gets an explicitly laid out interface type.
584 const glsl_type
*get_explicit_interface_type(bool supports_std430
) const;
586 /** Returns an explicitly laid out type given a type and size/align func
588 * The size/align func is only called for scalar and vector types and the
589 * returned type is otherwise laid out in the natural way as follows:
591 * - Arrays and matrices have a stride of ALIGN(elem_size, elem_align).
593 * - Structure types have their elements in-order and as tightly packed as
594 * possible following the alignment required by the size/align func.
596 * - All composite types (structures, matrices, and arrays) have an
597 * alignment equal to the highest alighment of any member of the composite.
599 * The types returned by this function are likely not suitable for most UBO
600 * or SSBO layout because they do not add the extra array and substructure
601 * alignment that is required by std140 and std430.
603 const glsl_type
*get_explicit_type_for_size_align(glsl_type_size_align_func type_info
,
604 unsigned *size
, unsigned *align
) const;
607 * Alignment in bytes of the start of this type in OpenCL memory.
609 unsigned cl_alignment() const;
612 * Size in bytes of this type in OpenCL memory
614 unsigned cl_size() const;
617 * Size in bytes of this type based on its explicit data.
619 * When using SPIR-V shaders (ARB_gl_spirv), memory layouts are expressed
620 * through explicit offset, stride and matrix layout, so the size
621 * can/should be computed used those values.
623 * Note that the value returned by this method is only correct if such
624 * values are set, so only with SPIR-V shaders. Should not be used with
627 unsigned explicit_size(bool align_to_stride
=false) const;
630 * \brief Can this type be implicitly converted to another?
632 * \return True if the types are identical or if this type can be converted
633 * to \c desired according to Section 4.1.10 of the GLSL spec.
636 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
637 * Implicit Conversions:
639 * In some situations, an expression and its type will be implicitly
640 * converted to a different type. The following table shows all allowed
641 * implicit conversions:
643 * Type of expression | Can be implicitly converted to
644 * --------------------------------------------------
657 * There are no implicit array or structure conversions. For example,
658 * an array of int cannot be implicitly converted to an array of float.
659 * There are no implicit conversions between signed and unsigned
663 bool can_implicitly_convert_to(const glsl_type
*desired
,
664 _mesa_glsl_parse_state
*state
) const;
667 * Query whether or not a type is a scalar (non-vector and non-matrix).
669 bool is_scalar() const
671 return (vector_elements
== 1)
672 && (base_type
>= GLSL_TYPE_UINT
)
673 && (base_type
<= GLSL_TYPE_IMAGE
);
677 * Query whether or not a type is a vector
679 bool is_vector() const
681 return (vector_elements
> 1)
682 && (matrix_columns
== 1)
683 && (base_type
>= GLSL_TYPE_UINT
)
684 && (base_type
<= GLSL_TYPE_BOOL
);
688 * Query whether or not a type is a matrix
690 bool is_matrix() const
692 /* GLSL only has float matrices. */
693 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
||
694 base_type
== GLSL_TYPE_DOUBLE
||
695 base_type
== GLSL_TYPE_FLOAT16
);
699 * Query whether or not a type is a non-array numeric type
701 bool is_numeric() const
703 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_INT64
);
707 * Query whether or not a type is an integer.
709 bool is_integer() const
711 return glsl_base_type_is_integer(base_type
);
715 * Query whether or not a type is an 32-bit integer.
717 bool is_integer_32() const
719 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
723 * Query whether or not a type is a 64-bit integer.
725 bool is_integer_64() const
727 return base_type
== GLSL_TYPE_UINT64
|| base_type
== GLSL_TYPE_INT64
;
731 * Query whether or not a type is a 32-bit or 64-bit integer
733 bool is_integer_32_64() const
735 return is_integer_32() || is_integer_64();
739 * Query whether or not type is an integral type, or for struct and array
740 * types, contains an integral type.
742 bool contains_integer() const;
745 * Query whether or not type is a double type, or for struct, interface and
746 * array types, contains a double type.
748 bool contains_double() const;
751 * Query whether or not type is a 64-bit type, or for struct, interface and
752 * array types, contains a double type.
754 bool contains_64bit() const;
757 * Query whether or not a type is a float type
759 bool is_float() const
761 return base_type
== GLSL_TYPE_FLOAT
;
765 * Query whether or not a type is a double type
767 bool is_double() const
769 return base_type
== GLSL_TYPE_DOUBLE
;
773 * Query whether a 64-bit type takes two slots.
775 bool is_dual_slot() const
777 return is_64bit() && vector_elements
> 2;
781 * Query whether or not a type is 64-bit
783 bool is_64bit() const
785 return glsl_base_type_is_64bit(base_type
);
789 * Query whether or not a type is 16-bit
791 bool is_16bit() const
793 return glsl_base_type_is_16bit(base_type
);
797 * Query whether or not a type is 32-bit
799 bool is_32bit() const
801 return base_type
== GLSL_TYPE_UINT
||
802 base_type
== GLSL_TYPE_INT
||
803 base_type
== GLSL_TYPE_FLOAT
;
807 * Query whether or not a type is a non-array boolean type
809 bool is_boolean() const
811 return base_type
== GLSL_TYPE_BOOL
;
815 * Query whether or not a type is a sampler
817 bool is_sampler() const
819 return base_type
== GLSL_TYPE_SAMPLER
;
823 * Query whether or not type is a sampler, or for struct, interface and
824 * array types, contains a sampler.
826 bool contains_sampler() const;
829 * Query whether or not type is an array or for struct, interface and
830 * array types, contains an array.
832 bool contains_array() const;
835 * Get the Mesa texture target index for a sampler type.
837 gl_texture_index
sampler_index() const;
840 * Query whether or not type is an image, or for struct, interface and
841 * array types, contains an image.
843 bool contains_image() const;
846 * Query whether or not a type is an image
848 bool is_image() const
850 return base_type
== GLSL_TYPE_IMAGE
;
854 * Query whether or not a type is an array
856 bool is_array() const
858 return base_type
== GLSL_TYPE_ARRAY
;
861 bool is_array_of_arrays() const
863 return is_array() && fields
.array
->is_array();
867 * Query whether or not a type is a record
869 bool is_struct() const
871 return base_type
== GLSL_TYPE_STRUCT
;
875 * Query whether or not a type is an interface
877 bool is_interface() const
879 return base_type
== GLSL_TYPE_INTERFACE
;
883 * Query whether or not a type is the void type singleton.
887 return base_type
== GLSL_TYPE_VOID
;
891 * Query whether or not a type is the error type singleton.
893 bool is_error() const
895 return base_type
== GLSL_TYPE_ERROR
;
899 * Query if a type is unnamed/anonymous (named by the parser)
902 bool is_subroutine() const
904 return base_type
== GLSL_TYPE_SUBROUTINE
;
906 bool contains_subroutine() const;
908 bool is_anonymous() const
910 return !strncmp(name
, "#anon", 5);
914 * Get the type stripped of any arrays
917 * Pointer to the type of elements of the first non-array type for array
918 * types, or pointer to itself for non-array types.
920 const glsl_type
*without_array() const
922 const glsl_type
*t
= this;
924 while (t
->is_array())
931 * Return the total number of elements in an array including the elements
932 * in arrays of arrays.
934 unsigned arrays_of_arrays_size() const
939 unsigned size
= length
;
940 const glsl_type
*base_type
= fields
.array
;
942 while (base_type
->is_array()) {
943 size
= size
* base_type
->length
;
944 base_type
= base_type
->fields
.array
;
950 * Return bit size for this type.
952 unsigned bit_size() const
954 return glsl_base_type_bit_size(this->base_type
);
959 * Query whether or not a type is an atomic_uint.
961 bool is_atomic_uint() const
963 return base_type
== GLSL_TYPE_ATOMIC_UINT
;
967 * Return the amount of atomic counter storage required for a type.
969 unsigned atomic_size() const
971 if (is_atomic_uint())
972 return ATOMIC_COUNTER_SIZE
;
974 return length
* fields
.array
->atomic_size();
980 * Return whether a type contains any atomic counters.
982 bool contains_atomic() const
984 return atomic_size() > 0;
988 * Return whether a type contains any opaque types.
990 bool contains_opaque() const;
993 * Query the full type of a matrix row
996 * If the type is not a matrix, \c glsl_type::error_type is returned.
997 * Otherwise a type matching the rows of the matrix is returned.
999 const glsl_type
*row_type() const
1004 if (explicit_stride
&& !interface_row_major
)
1005 return get_instance(base_type
, matrix_columns
, 1, explicit_stride
);
1007 return get_instance(base_type
, matrix_columns
, 1);
1011 * Query the full type of a matrix column
1014 * If the type is not a matrix, \c glsl_type::error_type is returned.
1015 * Otherwise a type matching the columns of the matrix is returned.
1017 const glsl_type
*column_type() const
1022 if (explicit_stride
&& interface_row_major
)
1023 return get_instance(base_type
, vector_elements
, 1, explicit_stride
);
1025 return get_instance(base_type
, vector_elements
, 1);
1029 * Get the type of a structure field
1032 * Pointer to the type of the named field. If the type is not a structure
1033 * or the named field does not exist, \c glsl_type::error_type is returned.
1035 const glsl_type
*field_type(const char *name
) const;
1038 * Get the location of a field within a record type
1040 int field_index(const char *name
) const;
1043 * Query the number of elements in an array type
1046 * The number of elements in the array for array types or -1 for non-array
1047 * types. If the number of elements in the array has not yet been declared,
1050 int array_size() const
1052 return is_array() ? length
: -1;
1056 * Query whether the array size for all dimensions has been declared.
1058 bool is_unsized_array() const
1060 return is_array() && length
== 0;
1064 * Return the number of coordinate components needed for this
1065 * sampler or image type.
1067 * This is based purely on the sampler's dimensionality. For example, this
1068 * returns 1 for sampler1D, and 3 for sampler2DArray.
1070 * Note that this is often different than actual coordinate type used in
1071 * a texturing built-in function, since those pack additional values (such
1072 * as the shadow comparator or projector) into the coordinate type.
1074 int coordinate_components() const;
1077 * Compares whether this type matches another type without taking into
1078 * account the precision in structures.
1080 * This is applied recursively so that structures containing structure
1081 * members can also ignore the precision.
1083 bool compare_no_precision(const glsl_type
*b
) const;
1086 * Compare a record type against another record type.
1088 * This is useful for matching record types declared on the same shader
1089 * stage as well as across different shader stages.
1090 * The option to not match name is needed for matching record types
1091 * declared across different shader stages.
1092 * The option to not match locations is to deal with places where the
1093 * same struct is defined in a block which has a location set on it.
1095 bool record_compare(const glsl_type
*b
, bool match_name
,
1096 bool match_locations
= true,
1097 bool match_precision
= true) const;
1100 * Get the type interface packing.
1102 enum glsl_interface_packing
get_interface_packing() const
1104 return (enum glsl_interface_packing
)interface_packing
;
1108 * Get the type interface packing used internally. For shared and packing
1109 * layouts this is implementation defined.
1111 enum glsl_interface_packing
get_internal_ifc_packing(bool std430_supported
) const
1113 enum glsl_interface_packing packing
= this->get_interface_packing();
1114 if (packing
== GLSL_INTERFACE_PACKING_STD140
||
1115 (!std430_supported
&&
1116 (packing
== GLSL_INTERFACE_PACKING_SHARED
||
1117 packing
== GLSL_INTERFACE_PACKING_PACKED
))) {
1118 return GLSL_INTERFACE_PACKING_STD140
;
1120 assert(packing
== GLSL_INTERFACE_PACKING_STD430
||
1121 (std430_supported
&&
1122 (packing
== GLSL_INTERFACE_PACKING_SHARED
||
1123 packing
== GLSL_INTERFACE_PACKING_PACKED
)));
1124 return GLSL_INTERFACE_PACKING_STD430
;
1129 * Check if the type interface is row major
1131 bool get_interface_row_major() const
1133 return (bool) interface_row_major
;
1140 static mtx_t hash_mutex
;
1143 * ralloc context for the type itself.
1147 /** Constructor for vector and matrix types */
1148 glsl_type(GLenum gl_type
,
1149 glsl_base_type base_type
, unsigned vector_elements
,
1150 unsigned matrix_columns
, const char *name
,
1151 unsigned explicit_stride
= 0, bool row_major
= false);
1153 /** Constructor for sampler or image types */
1154 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
1155 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
1156 glsl_base_type type
, const char *name
);
1158 /** Constructor for record types */
1159 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
1160 const char *name
, bool packed
= false);
1162 /** Constructor for interface types */
1163 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
1164 enum glsl_interface_packing packing
,
1165 bool row_major
, const char *name
);
1167 /** Constructor for interface types */
1168 glsl_type(const glsl_type
*return_type
,
1169 const glsl_function_param
*params
, unsigned num_params
);
1171 /** Constructors for array types */
1172 glsl_type(const glsl_type
*array
, unsigned length
, unsigned explicit_stride
);
1174 /** Constructor for subroutine types */
1175 glsl_type(const char *name
);
1177 /** Hash table containing the known explicit matrix and vector types. */
1178 static struct hash_table
*explicit_matrix_types
;
1180 /** Hash table containing the known array types. */
1181 static struct hash_table
*array_types
;
1183 /** Hash table containing the known struct types. */
1184 static struct hash_table
*struct_types
;
1186 /** Hash table containing the known interface types. */
1187 static struct hash_table
*interface_types
;
1189 /** Hash table containing the known subroutine types. */
1190 static struct hash_table
*subroutine_types
;
1192 /** Hash table containing the known function types. */
1193 static struct hash_table
*function_types
;
1195 static bool record_key_compare(const void *a
, const void *b
);
1196 static unsigned record_key_hash(const void *key
);
1199 * \name Built-in type flyweights
1203 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
1205 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
1206 #include "compiler/builtin_type_macros.h"
1210 * \name Friend functions.
1212 * These functions are friends because they must have C linkage and the
1213 * need to call various private methods or access various private static
1217 friend void glsl_type_singleton_init_or_ref(void);
1218 friend void glsl_type_singleton_decref(void);
1219 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
1225 #endif /* __cplusplus */
1227 struct glsl_struct_field
{
1228 const struct glsl_type
*type
;
1232 * For interface blocks, gl_varying_slot corresponding to the input/output
1233 * if this is a built-in input/output (i.e. a member of the built-in
1234 * gl_PerVertex interface block); -1 otherwise.
1236 * Ignored for structs.
1241 * For interface blocks, members may have an explicit byte offset
1242 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
1244 * Unless used for xfb_offset this field is ignored for structs.
1249 * For interface blocks, members may define a transform feedback buffer;
1255 * For interface blocks, members may define a transform feedback stride;
1261 * For interface blocks, the interpolation mode (as in
1262 * ir_variable::interpolation). 0 otherwise.
1264 unsigned interpolation
:3;
1267 * For interface blocks, 1 if this variable uses centroid interpolation (as
1268 * in ir_variable::centroid). 0 otherwise.
1270 unsigned centroid
:1;
1273 * For interface blocks, 1 if this variable uses sample interpolation (as
1274 * in ir_variable::sample). 0 otherwise.
1279 * Layout of the matrix. Uses glsl_matrix_layout values.
1281 unsigned matrix_layout
:2;
1284 * For interface blocks, 1 if this variable is a per-patch input or output
1285 * (as in ir_variable::patch). 0 otherwise.
1290 * Precision qualifier
1292 unsigned precision
:2;
1295 * Memory qualifiers, applicable to buffer variables defined in shader
1296 * storage buffer objects (SSBOs)
1298 unsigned memory_read_only
:1;
1299 unsigned memory_write_only
:1;
1300 unsigned memory_coherent
:1;
1301 unsigned memory_volatile
:1;
1302 unsigned memory_restrict
:1;
1305 * Layout format, applicable to image variables only.
1307 enum pipe_format image_format
;
1310 * Any of the xfb_* qualifiers trigger the shader to be in transform
1311 * feedback mode so we need to keep track of whether the buffer was
1312 * explicitly set or if its just been assigned the default global value.
1314 unsigned explicit_xfb_buffer
:1;
1316 unsigned implicit_sized_array
:1;
1318 #define DEFAULT_CONSTRUCTORS(_type, _precision, _name) \
1319 type(_type), name(_name), location(-1), offset(-1), xfb_buffer(0), \
1320 xfb_stride(0), interpolation(0), centroid(0), \
1321 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED), patch(0), \
1322 precision(_precision), memory_read_only(0), \
1323 memory_write_only(0), memory_coherent(0), memory_volatile(0), \
1324 memory_restrict(0), image_format(PIPE_FORMAT_NONE), \
1325 explicit_xfb_buffer(0), \
1326 implicit_sized_array(0)
1328 glsl_struct_field(const struct glsl_type
*_type
,
1331 : DEFAULT_CONSTRUCTORS(_type
, _precision
, _name
)
1336 glsl_struct_field(const struct glsl_type
*_type
, const char *_name
)
1337 : DEFAULT_CONSTRUCTORS(_type
, GLSL_PRECISION_NONE
, _name
)
1343 : DEFAULT_CONSTRUCTORS(NULL
, GLSL_PRECISION_NONE
, NULL
)
1347 #undef DEFAULT_CONSTRUCTORS
1351 struct glsl_function_param
{
1352 const struct glsl_type
*type
;
1358 static inline unsigned int
1359 glsl_align(unsigned int a
, unsigned int align
)
1361 return (a
+ align
- 1) / align
* align
;
1364 #endif /* GLSL_TYPES_H */