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.
35 struct _mesa_glsl_parse_state
;
36 struct glsl_symbol_table
;
39 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
);
42 _mesa_glsl_release_types(void);
49 /* Note: GLSL_TYPE_UINT, GLSL_TYPE_INT, and GLSL_TYPE_FLOAT must be 0, 1,
50 * and 2 so that they will fit in the 2 bits of glsl_type::sampled_type.
61 GLSL_TYPE_ATOMIC_UINT
,
71 static inline bool glsl_base_type_is_64bit(enum glsl_base_type type
)
73 return type
== GLSL_TYPE_DOUBLE
||
74 type
== GLSL_TYPE_UINT64
||
75 type
== GLSL_TYPE_INT64
||
76 type
== GLSL_TYPE_IMAGE
||
77 type
== GLSL_TYPE_SAMPLER
;
80 static inline bool glsl_base_type_is_integer(enum glsl_base_type type
)
82 return type
== GLSL_TYPE_UINT
||
83 type
== GLSL_TYPE_INT
||
84 type
== GLSL_TYPE_UINT64
||
85 type
== GLSL_TYPE_INT64
||
86 type
== GLSL_TYPE_BOOL
||
87 type
== GLSL_TYPE_SAMPLER
||
88 type
== GLSL_TYPE_IMAGE
;
91 enum glsl_sampler_dim
{
92 GLSL_SAMPLER_DIM_1D
= 0,
95 GLSL_SAMPLER_DIM_CUBE
,
96 GLSL_SAMPLER_DIM_RECT
,
98 GLSL_SAMPLER_DIM_EXTERNAL
,
100 GLSL_SAMPLER_DIM_SUBPASS
, /* for vulkan input attachments */
101 GLSL_SAMPLER_DIM_SUBPASS_MS
, /* for multisampled vulkan input attachments */
104 enum glsl_interface_packing
{
105 GLSL_INTERFACE_PACKING_STD140
,
106 GLSL_INTERFACE_PACKING_SHARED
,
107 GLSL_INTERFACE_PACKING_PACKED
,
108 GLSL_INTERFACE_PACKING_STD430
111 enum glsl_matrix_layout
{
113 * The layout of the matrix is inherited from the object containing the
114 * matrix (the top level structure or the uniform block).
116 GLSL_MATRIX_LAYOUT_INHERITED
,
119 * Explicit column-major layout
121 * If a uniform block doesn't have an explicit layout set, it will default
124 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
,
129 GLSL_MATRIX_LAYOUT_ROW_MAJOR
133 GLSL_PRECISION_NONE
= 0,
135 GLSL_PRECISION_MEDIUM
,
141 #include "util/ralloc.h"
142 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
146 glsl_base_type base_type
;
148 unsigned sampler_dimensionality
:4; /**< \see glsl_sampler_dim */
149 unsigned sampler_shadow
:1;
150 unsigned sampler_array
:1;
151 unsigned sampled_type
:2; /**< Type of data returned using this
152 * sampler or image. Only \c
153 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
154 * and \c GLSL_TYPE_UINT are valid.
156 unsigned interface_packing
:2;
157 unsigned interface_row_major
:1;
159 /* Callers of this ralloc-based new need not call delete. It's
160 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
161 static void* operator new(size_t size
)
163 mtx_lock(&glsl_type::mem_mutex
);
165 /* mem_ctx should have been created by the static members */
166 assert(glsl_type::mem_ctx
!= NULL
);
170 type
= ralloc_size(glsl_type::mem_ctx
, size
);
171 assert(type
!= NULL
);
173 mtx_unlock(&glsl_type::mem_mutex
);
178 /* If the user *does* call delete, that's OK, we will just
179 * ralloc_free in that case. */
180 static void operator delete(void *type
)
182 mtx_lock(&glsl_type::mem_mutex
);
184 mtx_unlock(&glsl_type::mem_mutex
);
188 * \name Vector and matrix element counts
190 * For scalars, each of these values will be 1. For non-numeric types
194 uint8_t vector_elements
; /**< 1, 2, 3, or 4 vector elements. */
195 uint8_t matrix_columns
; /**< 1, 2, 3, or 4 matrix columns. */
199 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
200 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
201 * elements in the structure and the number of values pointed to by
202 * \c fields.structure (below).
207 * Name of the data type
209 * Will never be \c NULL.
214 * Subtype of composite data types.
217 const struct glsl_type
*array
; /**< Type of array elements. */
218 struct glsl_function_param
*parameters
; /**< Parameters to function. */
219 struct glsl_struct_field
*structure
; /**< List of struct fields. */
223 * \name Pointers to various public type singletons
227 #define DECL_TYPE(NAME, ...) \
228 static const glsl_type *const NAME##_type;
230 #define STRUCT_TYPE(NAME) \
231 static const glsl_type *const struct_##NAME##_type;
232 #include "compiler/builtin_type_macros.h"
236 * Convenience accessors for vector types (shorter than get_instance()).
239 static const glsl_type
*vec(unsigned components
);
240 static const glsl_type
*dvec(unsigned components
);
241 static const glsl_type
*ivec(unsigned components
);
242 static const glsl_type
*uvec(unsigned components
);
243 static const glsl_type
*bvec(unsigned components
);
244 static const glsl_type
*i64vec(unsigned components
);
245 static const glsl_type
*u64vec(unsigned components
);
249 * For numeric and boolean derived types returns the basic scalar type
251 * If the type is a numeric or boolean scalar, vector, or matrix type,
252 * this function gets the scalar type of the individual components. For
253 * all other types, including arrays of numeric or boolean types, the
254 * error type is returned.
256 const glsl_type
*get_base_type() const;
259 * Get the basic scalar type which this type aggregates.
261 * If the type is a numeric or boolean scalar, vector, or matrix, or an
262 * array of any of those, this function gets the scalar type of the
263 * individual components. For structs and arrays of structs, this function
264 * returns the struct type. For samplers and arrays of samplers, this
265 * function returns the sampler type.
267 const glsl_type
*get_scalar_type() const;
270 * Get the instance of a built-in scalar, vector, or matrix type
272 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
276 * Get the instance of a sampler type
278 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
281 glsl_base_type type
);
283 static const glsl_type
*get_image_instance(enum glsl_sampler_dim dim
,
284 bool array
, glsl_base_type type
);
287 * Get the instance of an array type
289 static const glsl_type
*get_array_instance(const glsl_type
*base
,
293 * Get the instance of a record type
295 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
300 * Get the instance of an interface block type
302 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
304 enum glsl_interface_packing packing
,
306 const char *block_name
);
309 * Get the instance of an subroutine type
311 static const glsl_type
*get_subroutine_instance(const char *subroutine_name
);
314 * Get the instance of a function type
316 static const glsl_type
*get_function_instance(const struct glsl_type
*return_type
,
317 const glsl_function_param
*parameters
,
318 unsigned num_params
);
321 * Get the type resulting from a multiplication of \p type_a * \p type_b
323 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
324 const glsl_type
*type_b
);
327 * Query the total number of scalars that make up a scalar, vector or matrix
329 unsigned components() const
331 return vector_elements
* matrix_columns
;
335 * Calculate the number of components slots required to hold this type
337 * This is used to determine how many uniform or varying locations a type
340 unsigned component_slots() const;
343 * Calculate offset between the base location of the struct in
344 * uniform storage and a struct member.
345 * For the initial call, length is the index of the member to find the
348 unsigned record_location_offset(unsigned length
) const;
351 * Calculate the number of unique values from glGetUniformLocation for the
352 * elements of the type.
354 * This is used to allocate slots in the UniformRemapTable, the amount of
355 * locations may not match with actual used storage space by the driver.
357 unsigned uniform_locations() const;
360 * Used to count the number of varyings contained in the type ignoring
361 * innermost array elements.
363 unsigned varying_count() const;
366 * Calculate the number of attribute slots required to hold this type
368 * This implements the language rules of GLSL 1.50 for counting the number
369 * of slots used by a vertex attribute. It also determines the number of
370 * varying slots the type will use up in the absence of varying packing
371 * (and thus, it can be used to measure the number of varying slots used by
372 * the varyings that are generated by lower_packed_varyings).
374 * For vertex shader attributes - doubles only take one slot.
375 * For inter-shader varyings - dvec3/dvec4 take two slots.
377 unsigned count_attribute_slots(bool is_vertex_input
) const;
380 * Alignment in bytes of the start of this type in a std140 uniform
383 unsigned std140_base_alignment(bool row_major
) const;
385 /** Size in bytes of this type in a std140 uniform block.
387 * Note that this is not GL_UNIFORM_SIZE (which is the number of
388 * elements in the array)
390 unsigned std140_size(bool row_major
) const;
393 * Alignment in bytes of the start of this type in a std430 shader
396 unsigned std430_base_alignment(bool row_major
) const;
399 * Calculate array stride in bytes of this type in a std430 shader storage
402 unsigned std430_array_stride(bool row_major
) const;
405 * Size in bytes of this type in a std430 shader storage block.
407 * Note that this is not GL_BUFFER_SIZE
409 unsigned std430_size(bool row_major
) const;
412 * \brief Can this type be implicitly converted to another?
414 * \return True if the types are identical or if this type can be converted
415 * to \c desired according to Section 4.1.10 of the GLSL spec.
418 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
419 * Implicit Conversions:
421 * In some situations, an expression and its type will be implicitly
422 * converted to a different type. The following table shows all allowed
423 * implicit conversions:
425 * Type of expression | Can be implicitly converted to
426 * --------------------------------------------------
439 * There are no implicit array or structure conversions. For example,
440 * an array of int cannot be implicitly converted to an array of float.
441 * There are no implicit conversions between signed and unsigned
445 bool can_implicitly_convert_to(const glsl_type
*desired
,
446 _mesa_glsl_parse_state
*state
) const;
449 * Query whether or not a type is a scalar (non-vector and non-matrix).
451 bool is_scalar() const
453 return (vector_elements
== 1)
454 && (base_type
>= GLSL_TYPE_UINT
)
455 && (base_type
<= GLSL_TYPE_IMAGE
);
459 * Query whether or not a type is a vector
461 bool is_vector() const
463 return (vector_elements
> 1)
464 && (matrix_columns
== 1)
465 && (base_type
>= GLSL_TYPE_UINT
)
466 && (base_type
<= GLSL_TYPE_BOOL
);
470 * Query whether or not a type is a matrix
472 bool is_matrix() const
474 /* GLSL only has float matrices. */
475 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
|| base_type
== GLSL_TYPE_DOUBLE
);
479 * Query whether or not a type is a non-array numeric type
481 bool is_numeric() const
483 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_INT64
);
487 * Query whether or not a type is an integral type
489 bool is_integer() const
491 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
495 * Query whether or not a type is a 64-bit integer.
497 bool is_integer_64() const
499 return base_type
== GLSL_TYPE_UINT64
|| base_type
== GLSL_TYPE_INT64
;
503 * Query whether or not a type is a 32-bit or 64-bit integer
505 bool is_integer_32_64() const
507 return is_integer() || is_integer_64();
511 * Query whether or not type is an integral type, or for struct and array
512 * types, contains an integral type.
514 bool contains_integer() const;
517 * Query whether or not type is a double type, or for struct, interface and
518 * array types, contains a double type.
520 bool contains_double() const;
523 * Query whether or not a type is a float type
525 bool is_float() const
527 return base_type
== GLSL_TYPE_FLOAT
;
531 * Query whether or not a type is a double type
533 bool is_double() const
535 return base_type
== GLSL_TYPE_DOUBLE
;
539 * Query whether a 64-bit type takes two slots.
541 bool is_dual_slot() const
543 return is_64bit() && vector_elements
> 2;
547 * Query whether or not a type is 64-bit
549 bool is_64bit() const
551 return glsl_base_type_is_64bit(base_type
);
555 * Query whether or not a type is a non-array boolean type
557 bool is_boolean() const
559 return base_type
== GLSL_TYPE_BOOL
;
563 * Query whether or not a type is a sampler
565 bool is_sampler() const
567 return base_type
== GLSL_TYPE_SAMPLER
;
571 * Query whether or not type is a sampler, or for struct, interface and
572 * array types, contains a sampler.
574 bool contains_sampler() const;
577 * Query whether or not type is an array or for struct, interface and
578 * array types, contains an array.
580 bool contains_array() const;
583 * Get the Mesa texture target index for a sampler type.
585 gl_texture_index
sampler_index() const;
588 * Query whether or not type is an image, or for struct, interface and
589 * array types, contains an image.
591 bool contains_image() const;
594 * Query whether or not a type is an image
596 bool is_image() const
598 return base_type
== GLSL_TYPE_IMAGE
;
602 * Query whether or not a type is an array
604 bool is_array() const
606 return base_type
== GLSL_TYPE_ARRAY
;
609 bool is_array_of_arrays() const
611 return is_array() && fields
.array
->is_array();
615 * Query whether or not a type is a record
617 bool is_record() const
619 return base_type
== GLSL_TYPE_STRUCT
;
623 * Query whether or not a type is an interface
625 bool is_interface() const
627 return base_type
== GLSL_TYPE_INTERFACE
;
631 * Query whether or not a type is the void type singleton.
635 return base_type
== GLSL_TYPE_VOID
;
639 * Query whether or not a type is the error type singleton.
641 bool is_error() const
643 return base_type
== GLSL_TYPE_ERROR
;
647 * Query if a type is unnamed/anonymous (named by the parser)
650 bool is_subroutine() const
652 return base_type
== GLSL_TYPE_SUBROUTINE
;
654 bool contains_subroutine() const;
656 bool is_anonymous() const
658 return !strncmp(name
, "#anon", 5);
662 * Get the type stripped of any arrays
665 * Pointer to the type of elements of the first non-array type for array
666 * types, or pointer to itself for non-array types.
668 const glsl_type
*without_array() const
670 const glsl_type
*t
= this;
672 while (t
->is_array())
679 * Return the total number of elements in an array including the elements
680 * in arrays of arrays.
682 unsigned arrays_of_arrays_size() const
687 unsigned size
= length
;
688 const glsl_type
*base_type
= fields
.array
;
690 while (base_type
->is_array()) {
691 size
= size
* base_type
->length
;
692 base_type
= base_type
->fields
.array
;
698 * Query whether or not a type is an atomic_uint.
700 bool is_atomic_uint() const
702 return base_type
== GLSL_TYPE_ATOMIC_UINT
;
706 * Return the amount of atomic counter storage required for a type.
708 unsigned atomic_size() const
710 if (is_atomic_uint())
711 return ATOMIC_COUNTER_SIZE
;
713 return length
* fields
.array
->atomic_size();
719 * Return whether a type contains any atomic counters.
721 bool contains_atomic() const
723 return atomic_size() > 0;
727 * Return whether a type contains any opaque types.
729 bool contains_opaque() const;
732 * Query the full type of a matrix row
735 * If the type is not a matrix, \c glsl_type::error_type is returned.
736 * Otherwise a type matching the rows of the matrix is returned.
738 const glsl_type
*row_type() const
741 ? get_instance(base_type
, matrix_columns
, 1)
746 * Query the full type of a matrix column
749 * If the type is not a matrix, \c glsl_type::error_type is returned.
750 * Otherwise a type matching the columns of the matrix is returned.
752 const glsl_type
*column_type() const
755 ? get_instance(base_type
, vector_elements
, 1)
760 * Get the type of a structure field
763 * Pointer to the type of the named field. If the type is not a structure
764 * or the named field does not exist, \c glsl_type::error_type is returned.
766 const glsl_type
*field_type(const char *name
) const;
769 * Get the location of a field within a record type
771 int field_index(const char *name
) const;
774 * Query the number of elements in an array type
777 * The number of elements in the array for array types or -1 for non-array
778 * types. If the number of elements in the array has not yet been declared,
781 int array_size() const
783 return is_array() ? length
: -1;
787 * Query whether the array size for all dimensions has been declared.
789 bool is_unsized_array() const
791 return is_array() && length
== 0;
795 * Return the number of coordinate components needed for this
796 * sampler or image type.
798 * This is based purely on the sampler's dimensionality. For example, this
799 * returns 1 for sampler1D, and 3 for sampler2DArray.
801 * Note that this is often different than actual coordinate type used in
802 * a texturing built-in function, since those pack additional values (such
803 * as the shadow comparator or projector) into the coordinate type.
805 int coordinate_components() const;
808 * Compare a record type against another record type.
810 * This is useful for matching record types declared across shader stages.
811 * The option to not match locations is to deal with places where the
812 * same struct is defined in a block which has a location set on it.
814 bool record_compare(const glsl_type
*b
, bool match_locations
= true) const;
817 * Get the type interface packing.
819 enum glsl_interface_packing
get_interface_packing() const
821 return (enum glsl_interface_packing
)interface_packing
;
825 * Check if the type interface is row major
827 bool get_interface_row_major() const
829 return (bool) interface_row_major
;
834 static mtx_t mem_mutex
;
835 static mtx_t hash_mutex
;
838 * ralloc context for all glsl_type allocations
840 * Set on the first call to \c glsl_type::new.
842 static void *mem_ctx
;
844 void init_ralloc_type_ctx(void);
846 /** Constructor for vector and matrix types */
847 glsl_type(GLenum gl_type
,
848 glsl_base_type base_type
, unsigned vector_elements
,
849 unsigned matrix_columns
, const char *name
);
851 /** Constructor for sampler or image types */
852 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
853 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
854 unsigned type
, const char *name
);
856 /** Constructor for record types */
857 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
860 /** Constructor for interface types */
861 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
862 enum glsl_interface_packing packing
,
863 bool row_major
, const char *name
);
865 /** Constructor for interface types */
866 glsl_type(const glsl_type
*return_type
,
867 const glsl_function_param
*params
, unsigned num_params
);
869 /** Constructor for array types */
870 glsl_type(const glsl_type
*array
, unsigned length
);
872 /** Constructor for subroutine types */
873 glsl_type(const char *name
);
875 /** Hash table containing the known array types. */
876 static struct hash_table
*array_types
;
878 /** Hash table containing the known record types. */
879 static struct hash_table
*record_types
;
881 /** Hash table containing the known interface types. */
882 static struct hash_table
*interface_types
;
884 /** Hash table containing the known subroutine types. */
885 static struct hash_table
*subroutine_types
;
887 /** Hash table containing the known function types. */
888 static struct hash_table
*function_types
;
890 static bool record_key_compare(const void *a
, const void *b
);
891 static unsigned record_key_hash(const void *key
);
894 * \name Built-in type flyweights
898 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
900 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
901 #include "compiler/builtin_type_macros.h"
905 * \name Friend functions.
907 * These functions are friends because they must have C linkage and the
908 * need to call various private methods or access various private static
912 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
913 friend void _mesa_glsl_release_types(void);
919 #endif /* __cplusplus */
921 struct glsl_struct_field
{
922 const struct glsl_type
*type
;
926 * For interface blocks, gl_varying_slot corresponding to the input/output
927 * if this is a built-in input/output (i.e. a member of the built-in
928 * gl_PerVertex interface block); -1 otherwise.
930 * Ignored for structs.
935 * For interface blocks, members may have an explicit byte offset
936 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
938 * Unless used for xfb_offset this field is ignored for structs.
943 * For interface blocks, members may define a transform feedback buffer;
949 * For interface blocks, members may define a transform feedback stride;
955 * For interface blocks, the interpolation mode (as in
956 * ir_variable::interpolation). 0 otherwise.
958 unsigned interpolation
:2;
961 * For interface blocks, 1 if this variable uses centroid interpolation (as
962 * in ir_variable::centroid). 0 otherwise.
967 * For interface blocks, 1 if this variable uses sample interpolation (as
968 * in ir_variable::sample). 0 otherwise.
973 * Layout of the matrix. Uses glsl_matrix_layout values.
975 unsigned matrix_layout
:2;
978 * For interface blocks, 1 if this variable is a per-patch input or output
979 * (as in ir_variable::patch). 0 otherwise.
984 * Precision qualifier
986 unsigned precision
:2;
989 * Memory qualifiers, applicable to buffer variables defined in shader
990 * storage buffer objects (SSBOs)
992 unsigned memory_read_only
:1;
993 unsigned memory_write_only
:1;
994 unsigned memory_coherent
:1;
995 unsigned memory_volatile
:1;
996 unsigned memory_restrict
:1;
999 * Layout format, applicable to image variables only.
1001 unsigned image_format
:16;
1004 * Any of the xfb_* qualifiers trigger the shader to be in transform
1005 * feedback mode so we need to keep track of whether the buffer was
1006 * explicitly set or if its just been assigned the default global value.
1008 unsigned explicit_xfb_buffer
:1;
1010 unsigned implicit_sized_array
:1;
1012 glsl_struct_field(const struct glsl_type
*_type
, const char *_name
)
1013 : type(_type
), name(_name
), location(-1), offset(0), xfb_buffer(0),
1014 xfb_stride(0), interpolation(0), centroid(0),
1015 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED
), patch(0),
1016 precision(GLSL_PRECISION_NONE
), memory_read_only(0),
1017 memory_write_only(0), memory_coherent(0), memory_volatile(0),
1018 memory_restrict(0), image_format(0), explicit_xfb_buffer(0),
1019 implicit_sized_array(0)
1031 struct glsl_function_param
{
1032 const struct glsl_type
*type
;
1038 static inline unsigned int
1039 glsl_align(unsigned int a
, unsigned int align
)
1041 return (a
+ align
- 1) / align
* align
;
1044 #endif /* GLSL_TYPES_H */