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.
36 struct _mesa_glsl_parse_state
;
37 struct glsl_symbol_table
;
40 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
);
43 _mesa_glsl_release_types(void);
50 /* Note: GLSL_TYPE_UINT, GLSL_TYPE_INT, and GLSL_TYPE_FLOAT must be 0, 1,
51 * and 2 so that they will fit in the 2 bits of glsl_type::sampled_type.
60 GLSL_TYPE_ATOMIC_UINT
,
70 static inline bool glsl_base_type_is_64bit(enum glsl_base_type type
)
72 return type
== GLSL_TYPE_DOUBLE
;
75 enum glsl_sampler_dim
{
76 GLSL_SAMPLER_DIM_1D
= 0,
79 GLSL_SAMPLER_DIM_CUBE
,
80 GLSL_SAMPLER_DIM_RECT
,
82 GLSL_SAMPLER_DIM_EXTERNAL
,
84 GLSL_SAMPLER_DIM_SUBPASS
, /* for vulkan input attachments */
87 enum glsl_interface_packing
{
88 GLSL_INTERFACE_PACKING_STD140
,
89 GLSL_INTERFACE_PACKING_SHARED
,
90 GLSL_INTERFACE_PACKING_PACKED
,
91 GLSL_INTERFACE_PACKING_STD430
94 enum glsl_matrix_layout
{
96 * The layout of the matrix is inherited from the object containing the
97 * matrix (the top level structure or the uniform block).
99 GLSL_MATRIX_LAYOUT_INHERITED
,
102 * Explicit column-major layout
104 * If a uniform block doesn't have an explicit layout set, it will default
107 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
,
112 GLSL_MATRIX_LAYOUT_ROW_MAJOR
116 GLSL_PRECISION_NONE
= 0,
118 GLSL_PRECISION_MEDIUM
,
124 #include "util/ralloc.h"
125 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
129 glsl_base_type base_type
;
131 unsigned sampler_dimensionality
:4; /**< \see glsl_sampler_dim */
132 unsigned sampler_shadow
:1;
133 unsigned sampler_array
:1;
134 unsigned sampled_type
:2; /**< Type of data returned using this
135 * sampler or image. Only \c
136 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
137 * and \c GLSL_TYPE_UINT are valid.
139 unsigned interface_packing
:2;
140 unsigned interface_row_major
:1;
142 /* Callers of this ralloc-based new need not call delete. It's
143 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
144 static void* operator new(size_t size
)
146 mtx_lock(&glsl_type::mutex
);
148 /* mem_ctx should have been created by the static members */
149 assert(glsl_type::mem_ctx
!= NULL
);
153 type
= ralloc_size(glsl_type::mem_ctx
, size
);
154 assert(type
!= NULL
);
156 mtx_unlock(&glsl_type::mutex
);
161 /* If the user *does* call delete, that's OK, we will just
162 * ralloc_free in that case. */
163 static void operator delete(void *type
)
165 mtx_lock(&glsl_type::mutex
);
167 mtx_unlock(&glsl_type::mutex
);
171 * \name Vector and matrix element counts
173 * For scalars, each of these values will be 1. For non-numeric types
177 uint8_t vector_elements
; /**< 1, 2, 3, or 4 vector elements. */
178 uint8_t matrix_columns
; /**< 1, 2, 3, or 4 matrix columns. */
182 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
183 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
184 * elements in the structure and the number of values pointed to by
185 * \c fields.structure (below).
190 * Name of the data type
192 * Will never be \c NULL.
197 * Subtype of composite data types.
200 const struct glsl_type
*array
; /**< Type of array elements. */
201 struct glsl_function_param
*parameters
; /**< Parameters to function. */
202 struct glsl_struct_field
*structure
; /**< List of struct fields. */
206 * \name Pointers to various public type singletons
210 #define DECL_TYPE(NAME, ...) \
211 static const glsl_type *const NAME##_type;
213 #define STRUCT_TYPE(NAME) \
214 static const glsl_type *const struct_##NAME##_type;
215 #include "compiler/builtin_type_macros.h"
219 * Convenience accessors for vector types (shorter than get_instance()).
222 static const glsl_type
*vec(unsigned components
);
223 static const glsl_type
*dvec(unsigned components
);
224 static const glsl_type
*ivec(unsigned components
);
225 static const glsl_type
*uvec(unsigned components
);
226 static const glsl_type
*bvec(unsigned components
);
230 * For numeric and boolean derived types returns the basic scalar type
232 * If the type is a numeric or boolean scalar, vector, or matrix type,
233 * this function gets the scalar type of the individual components. For
234 * all other types, including arrays of numeric or boolean types, the
235 * error type is returned.
237 const glsl_type
*get_base_type() const;
240 * Get the basic scalar type which this type aggregates.
242 * If the type is a numeric or boolean scalar, vector, or matrix, or an
243 * array of any of those, this function gets the scalar type of the
244 * individual components. For structs and arrays of structs, this function
245 * returns the struct type. For samplers and arrays of samplers, this
246 * function returns the sampler type.
248 const glsl_type
*get_scalar_type() const;
251 * Get the instance of a built-in scalar, vector, or matrix type
253 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
257 * Get the instance of a sampler type
259 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
262 glsl_base_type type
);
264 static const glsl_type
*get_image_instance(enum glsl_sampler_dim dim
,
265 bool array
, glsl_base_type type
);
268 * Get the instance of an array type
270 static const glsl_type
*get_array_instance(const glsl_type
*base
,
274 * Get the instance of a record type
276 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
281 * Get the instance of an interface block type
283 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
285 enum glsl_interface_packing packing
,
287 const char *block_name
);
290 * Get the instance of an subroutine type
292 static const glsl_type
*get_subroutine_instance(const char *subroutine_name
);
295 * Get the instance of a function type
297 static const glsl_type
*get_function_instance(const struct glsl_type
*return_type
,
298 const glsl_function_param
*parameters
,
299 unsigned num_params
);
302 * Get the type resulting from a multiplication of \p type_a * \p type_b
304 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
305 const glsl_type
*type_b
);
308 * Query the total number of scalars that make up a scalar, vector or matrix
310 unsigned components() const
312 return vector_elements
* matrix_columns
;
316 * Calculate the number of components slots required to hold this type
318 * This is used to determine how many uniform or varying locations a type
321 unsigned component_slots() const;
324 * Calculate offset between the base location of the struct in
325 * uniform storage and a struct member.
326 * For the initial call, length is the index of the member to find the
329 unsigned record_location_offset(unsigned length
) const;
332 * Calculate the number of unique values from glGetUniformLocation for the
333 * elements of the type.
335 * This is used to allocate slots in the UniformRemapTable, the amount of
336 * locations may not match with actual used storage space by the driver.
338 unsigned uniform_locations() const;
341 * Used to count the number of varyings contained in the type ignoring
342 * innermost array elements.
344 unsigned varying_count() const;
347 * Calculate the number of attribute slots required to hold this type
349 * This implements the language rules of GLSL 1.50 for counting the number
350 * of slots used by a vertex attribute. It also determines the number of
351 * varying slots the type will use up in the absence of varying packing
352 * (and thus, it can be used to measure the number of varying slots used by
353 * the varyings that are generated by lower_packed_varyings).
355 * For vertex shader attributes - doubles only take one slot.
356 * For inter-shader varyings - dvec3/dvec4 take two slots.
358 unsigned count_attribute_slots(bool is_vertex_input
) const;
361 * Alignment in bytes of the start of this type in a std140 uniform
364 unsigned std140_base_alignment(bool row_major
) const;
366 /** Size in bytes of this type in a std140 uniform block.
368 * Note that this is not GL_UNIFORM_SIZE (which is the number of
369 * elements in the array)
371 unsigned std140_size(bool row_major
) const;
374 * Alignment in bytes of the start of this type in a std430 shader
377 unsigned std430_base_alignment(bool row_major
) const;
380 * Calculate array stride in bytes of this type in a std430 shader storage
383 unsigned std430_array_stride(bool row_major
) const;
386 * Size in bytes of this type in a std430 shader storage block.
388 * Note that this is not GL_BUFFER_SIZE
390 unsigned std430_size(bool row_major
) const;
393 * \brief Can this type be implicitly converted to another?
395 * \return True if the types are identical or if this type can be converted
396 * to \c desired according to Section 4.1.10 of the GLSL spec.
399 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
400 * Implicit Conversions:
402 * In some situations, an expression and its type will be implicitly
403 * converted to a different type. The following table shows all allowed
404 * implicit conversions:
406 * Type of expression | Can be implicitly converted to
407 * --------------------------------------------------
420 * There are no implicit array or structure conversions. For example,
421 * an array of int cannot be implicitly converted to an array of float.
422 * There are no implicit conversions between signed and unsigned
426 bool can_implicitly_convert_to(const glsl_type
*desired
,
427 _mesa_glsl_parse_state
*state
) const;
430 * Query whether or not a type is a scalar (non-vector and non-matrix).
432 bool is_scalar() const
434 return (vector_elements
== 1)
435 && (base_type
>= GLSL_TYPE_UINT
)
436 && (base_type
<= GLSL_TYPE_BOOL
);
440 * Query whether or not a type is a vector
442 bool is_vector() const
444 return (vector_elements
> 1)
445 && (matrix_columns
== 1)
446 && (base_type
>= GLSL_TYPE_UINT
)
447 && (base_type
<= GLSL_TYPE_BOOL
);
451 * Query whether or not a type is a matrix
453 bool is_matrix() const
455 /* GLSL only has float matrices. */
456 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
|| base_type
== GLSL_TYPE_DOUBLE
);
460 * Query whether or not a type is a non-array numeric type
462 bool is_numeric() const
464 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_DOUBLE
);
468 * Query whether or not a type is an integral type
470 bool is_integer() const
472 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
476 * Query whether or not type is an integral type, or for struct, interface
477 * and array types, contains an integral type.
479 bool contains_integer() const;
482 * Query whether or not type is a double type, or for struct, interface and
483 * array types, contains a double type.
485 bool contains_double() const;
488 * Query whether or not a type is a float type
490 bool is_float() const
492 return base_type
== GLSL_TYPE_FLOAT
;
496 * Query whether or not a type is a double type
498 bool is_double() const
500 return base_type
== GLSL_TYPE_DOUBLE
;
504 * Query whether a 64-bit type takes two slots.
506 bool is_dual_slot() const
508 return is_64bit() && vector_elements
> 2;
512 * Query whether or not a type is 64-bit
514 bool is_64bit() const
516 return glsl_base_type_is_64bit(base_type
);
520 * Query whether or not a type is a non-array boolean type
522 bool is_boolean() const
524 return base_type
== GLSL_TYPE_BOOL
;
528 * Query whether or not a type is a sampler
530 bool is_sampler() const
532 return base_type
== GLSL_TYPE_SAMPLER
;
536 * Query whether or not type is a sampler, or for struct, interface and
537 * array types, contains a sampler.
539 bool contains_sampler() const;
542 * Get the Mesa texture target index for a sampler type.
544 gl_texture_index
sampler_index() const;
547 * Query whether or not type is an image, or for struct, interface and
548 * array types, contains an image.
550 bool contains_image() const;
553 * Query whether or not a type is an image
555 bool is_image() const
557 return base_type
== GLSL_TYPE_IMAGE
;
561 * Query whether or not a type is an array
563 bool is_array() const
565 return base_type
== GLSL_TYPE_ARRAY
;
568 bool is_array_of_arrays() const
570 return is_array() && fields
.array
->is_array();
574 * Query whether or not a type is a record
576 bool is_record() const
578 return base_type
== GLSL_TYPE_STRUCT
;
582 * Query whether or not a type is an interface
584 bool is_interface() const
586 return base_type
== GLSL_TYPE_INTERFACE
;
590 * Query whether or not a type is the void type singleton.
594 return base_type
== GLSL_TYPE_VOID
;
598 * Query whether or not a type is the error type singleton.
600 bool is_error() const
602 return base_type
== GLSL_TYPE_ERROR
;
606 * Query if a type is unnamed/anonymous (named by the parser)
609 bool is_subroutine() const
611 return base_type
== GLSL_TYPE_SUBROUTINE
;
613 bool contains_subroutine() const;
615 bool is_anonymous() const
617 return !strncmp(name
, "#anon", 5);
621 * Get the type stripped of any arrays
624 * Pointer to the type of elements of the first non-array type for array
625 * types, or pointer to itself for non-array types.
627 const glsl_type
*without_array() const
629 const glsl_type
*t
= this;
631 while (t
->is_array())
638 * Return the total number of elements in an array including the elements
639 * in arrays of arrays.
641 unsigned arrays_of_arrays_size() const
646 unsigned size
= length
;
647 const glsl_type
*base_type
= fields
.array
;
649 while (base_type
->is_array()) {
650 size
= size
* base_type
->length
;
651 base_type
= base_type
->fields
.array
;
657 * Return the amount of atomic counter storage required for a type.
659 unsigned atomic_size() const
661 if (base_type
== GLSL_TYPE_ATOMIC_UINT
)
662 return ATOMIC_COUNTER_SIZE
;
664 return length
* fields
.array
->atomic_size();
670 * Return whether a type contains any atomic counters.
672 bool contains_atomic() const
674 return atomic_size() > 0;
678 * Return whether a type contains any opaque types.
680 bool contains_opaque() const;
683 * Query the full type of a matrix row
686 * If the type is not a matrix, \c glsl_type::error_type is returned.
687 * Otherwise a type matching the rows of the matrix is returned.
689 const glsl_type
*row_type() const
692 ? get_instance(base_type
, matrix_columns
, 1)
697 * Query the full type of a matrix column
700 * If the type is not a matrix, \c glsl_type::error_type is returned.
701 * Otherwise a type matching the columns of the matrix is returned.
703 const glsl_type
*column_type() const
706 ? get_instance(base_type
, vector_elements
, 1)
711 * Get the type of a structure field
714 * Pointer to the type of the named field. If the type is not a structure
715 * or the named field does not exist, \c glsl_type::error_type is returned.
717 const glsl_type
*field_type(const char *name
) const;
720 * Get the location of a field within a record type
722 int field_index(const char *name
) const;
725 * Query the number of elements in an array type
728 * The number of elements in the array for array types or -1 for non-array
729 * types. If the number of elements in the array has not yet been declared,
732 int array_size() const
734 return is_array() ? length
: -1;
738 * Query whether the array size for all dimensions has been declared.
740 bool is_unsized_array() const
742 return is_array() && length
== 0;
746 * Return the number of coordinate components needed for this
747 * sampler or image type.
749 * This is based purely on the sampler's dimensionality. For example, this
750 * returns 1 for sampler1D, and 3 for sampler2DArray.
752 * Note that this is often different than actual coordinate type used in
753 * a texturing built-in function, since those pack additional values (such
754 * as the shadow comparitor or projector) into the coordinate type.
756 int coordinate_components() const;
759 * Compare a record type against another record type.
761 * This is useful for matching record types declared across shader stages.
762 * The option to not match locations is to deal with places where the
763 * same struct is defined in a block which has a location set on it.
765 bool record_compare(const glsl_type
*b
, bool match_locations
= true) const;
768 * Get the type interface packing.
770 enum glsl_interface_packing
get_interface_packing() const
772 return (enum glsl_interface_packing
)interface_packing
;
776 * Check if the type interface is row major
778 bool get_interface_row_major() const
780 return (bool) interface_row_major
;
788 * ralloc context for all glsl_type allocations
790 * Set on the first call to \c glsl_type::new.
792 static void *mem_ctx
;
794 void init_ralloc_type_ctx(void);
796 /** Constructor for vector and matrix types */
797 glsl_type(GLenum gl_type
,
798 glsl_base_type base_type
, unsigned vector_elements
,
799 unsigned matrix_columns
, const char *name
);
801 /** Constructor for sampler or image types */
802 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
803 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
804 unsigned type
, const char *name
);
806 /** Constructor for record types */
807 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
810 /** Constructor for interface types */
811 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
812 enum glsl_interface_packing packing
,
813 bool row_major
, const char *name
);
815 /** Constructor for interface types */
816 glsl_type(const glsl_type
*return_type
,
817 const glsl_function_param
*params
, unsigned num_params
);
819 /** Constructor for array types */
820 glsl_type(const glsl_type
*array
, unsigned length
);
822 /** Constructor for subroutine types */
823 glsl_type(const char *name
);
825 /** Hash table containing the known array types. */
826 static struct hash_table
*array_types
;
828 /** Hash table containing the known record types. */
829 static struct hash_table
*record_types
;
831 /** Hash table containing the known interface types. */
832 static struct hash_table
*interface_types
;
834 /** Hash table containing the known subroutine types. */
835 static struct hash_table
*subroutine_types
;
837 /** Hash table containing the known function types. */
838 static struct hash_table
*function_types
;
840 static bool record_key_compare(const void *a
, const void *b
);
841 static unsigned record_key_hash(const void *key
);
844 * \name Built-in type flyweights
848 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
850 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
851 #include "compiler/builtin_type_macros.h"
855 * \name Friend functions.
857 * These functions are friends because they must have C linkage and the
858 * need to call various private methods or access various private static
862 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
863 friend void _mesa_glsl_release_types(void);
869 #endif /* __cplusplus */
871 struct glsl_struct_field
{
872 const struct glsl_type
*type
;
876 * For interface blocks, gl_varying_slot corresponding to the input/output
877 * if this is a built-in input/output (i.e. a member of the built-in
878 * gl_PerVertex interface block); -1 otherwise.
880 * Ignored for structs.
885 * For interface blocks, members may have an explicit byte offset
886 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
888 * Unless used for xfb_offset this field is ignored for structs.
893 * For interface blocks, members may define a transform feedback buffer;
899 * For interface blocks, members may define a transform feedback stride;
905 * For interface blocks, the interpolation mode (as in
906 * ir_variable::interpolation). 0 otherwise.
908 unsigned interpolation
:2;
911 * For interface blocks, 1 if this variable uses centroid interpolation (as
912 * in ir_variable::centroid). 0 otherwise.
917 * For interface blocks, 1 if this variable uses sample interpolation (as
918 * in ir_variable::sample). 0 otherwise.
923 * Layout of the matrix. Uses glsl_matrix_layout values.
925 unsigned matrix_layout
:2;
928 * For interface blocks, 1 if this variable is a per-patch input or output
929 * (as in ir_variable::patch). 0 otherwise.
934 * Precision qualifier
936 unsigned precision
:2;
939 * Image qualifiers, applicable to buffer variables defined in shader
940 * storage buffer objects (SSBOs)
942 unsigned image_read_only
:1;
943 unsigned image_write_only
:1;
944 unsigned image_coherent
:1;
945 unsigned image_volatile
:1;
946 unsigned image_restrict
:1;
949 * Any of the xfb_* qualifiers trigger the shader to be in transform
950 * feedback mode so we need to keep track of whether the buffer was
951 * explicitly set or if its just been assigned the default global value.
953 unsigned explicit_xfb_buffer
:1;
955 unsigned implicit_sized_array
:1;
957 glsl_struct_field(const struct glsl_type
*_type
, const char *_name
)
958 : type(_type
), name(_name
), location(-1), interpolation(0), centroid(0),
959 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED
), patch(0),
960 precision(GLSL_PRECISION_NONE
), image_read_only(0), image_write_only(0),
961 image_coherent(0), image_volatile(0), image_restrict(0)
973 struct glsl_function_param
{
974 const struct glsl_type
*type
;
980 static inline unsigned int
981 glsl_align(unsigned int a
, unsigned int align
)
983 return (a
+ align
- 1) / align
* align
;
986 #endif /* GLSL_TYPES_H */