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);
57 GLSL_TYPE_ATOMIC_UINT
,
67 static inline bool glsl_base_type_is_64bit(enum glsl_base_type type
)
69 return type
== GLSL_TYPE_DOUBLE
;
72 enum glsl_sampler_dim
{
73 GLSL_SAMPLER_DIM_1D
= 0,
76 GLSL_SAMPLER_DIM_CUBE
,
77 GLSL_SAMPLER_DIM_RECT
,
79 GLSL_SAMPLER_DIM_EXTERNAL
,
83 enum glsl_interface_packing
{
84 GLSL_INTERFACE_PACKING_STD140
,
85 GLSL_INTERFACE_PACKING_SHARED
,
86 GLSL_INTERFACE_PACKING_PACKED
,
87 GLSL_INTERFACE_PACKING_STD430
90 enum glsl_matrix_layout
{
92 * The layout of the matrix is inherited from the object containing the
93 * matrix (the top level structure or the uniform block).
95 GLSL_MATRIX_LAYOUT_INHERITED
,
98 * Explicit column-major layout
100 * If a uniform block doesn't have an explicit layout set, it will default
103 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
,
108 GLSL_MATRIX_LAYOUT_ROW_MAJOR
112 GLSL_PRECISION_NONE
= 0,
114 GLSL_PRECISION_MEDIUM
,
120 #include "util/ralloc.h"
121 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
125 glsl_base_type base_type
;
127 unsigned sampler_dimensionality
:3; /**< \see glsl_sampler_dim */
128 unsigned sampler_shadow
:1;
129 unsigned sampler_array
:1;
130 unsigned sampled_type
:2; /**< Type of data returned using this
131 * sampler or image. Only \c
132 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
133 * and \c GLSL_TYPE_UINT are valid.
135 unsigned interface_packing
:2;
137 /* Callers of this ralloc-based new need not call delete. It's
138 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
139 static void* operator new(size_t size
)
141 mtx_lock(&glsl_type::mutex
);
143 /* mem_ctx should have been created by the static members */
144 assert(glsl_type::mem_ctx
!= NULL
);
148 type
= ralloc_size(glsl_type::mem_ctx
, size
);
149 assert(type
!= NULL
);
151 mtx_unlock(&glsl_type::mutex
);
156 /* If the user *does* call delete, that's OK, we will just
157 * ralloc_free in that case. */
158 static void operator delete(void *type
)
160 mtx_lock(&glsl_type::mutex
);
162 mtx_unlock(&glsl_type::mutex
);
166 * \name Vector and matrix element counts
168 * For scalars, each of these values will be 1. For non-numeric types
172 uint8_t vector_elements
; /**< 1, 2, 3, or 4 vector elements. */
173 uint8_t matrix_columns
; /**< 1, 2, 3, or 4 matrix columns. */
177 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
178 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
179 * elements in the structure and the number of values pointed to by
180 * \c fields.structure (below).
185 * Name of the data type
187 * Will never be \c NULL.
192 * Subtype of composite data types.
195 const struct glsl_type
*array
; /**< Type of array elements. */
196 struct glsl_function_param
*parameters
; /**< Parameters to function. */
197 struct glsl_struct_field
*structure
; /**< List of struct fields. */
201 * \name Pointers to various public type singletons
205 #define DECL_TYPE(NAME, ...) \
206 static const glsl_type *const NAME##_type;
208 #define STRUCT_TYPE(NAME) \
209 static const glsl_type *const struct_##NAME##_type;
210 #include "compiler/builtin_type_macros.h"
214 * Convenience accessors for vector types (shorter than get_instance()).
217 static const glsl_type
*vec(unsigned components
);
218 static const glsl_type
*dvec(unsigned components
);
219 static const glsl_type
*ivec(unsigned components
);
220 static const glsl_type
*uvec(unsigned components
);
221 static const glsl_type
*bvec(unsigned components
);
225 * For numeric and boolean derived types returns the basic scalar type
227 * If the type is a numeric or boolean scalar, vector, or matrix type,
228 * this function gets the scalar type of the individual components. For
229 * all other types, including arrays of numeric or boolean types, the
230 * error type is returned.
232 const glsl_type
*get_base_type() const;
235 * Get the basic scalar type which this type aggregates.
237 * If the type is a numeric or boolean scalar, vector, or matrix, or an
238 * array of any of those, this function gets the scalar type of the
239 * individual components. For structs and arrays of structs, this function
240 * returns the struct type. For samplers and arrays of samplers, this
241 * function returns the sampler type.
243 const glsl_type
*get_scalar_type() const;
246 * Get the instance of a built-in scalar, vector, or matrix type
248 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
252 * Get the instance of a sampler type
254 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
257 glsl_base_type type
);
259 static const glsl_type
*get_image_instance(enum glsl_sampler_dim dim
,
260 bool array
, glsl_base_type type
);
263 * Get the instance of an array type
265 static const glsl_type
*get_array_instance(const glsl_type
*base
,
269 * Get the instance of a record type
271 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
276 * Get the instance of an interface block type
278 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
280 enum glsl_interface_packing packing
,
281 const char *block_name
);
284 * Get the instance of an subroutine type
286 static const glsl_type
*get_subroutine_instance(const char *subroutine_name
);
289 * Get the instance of a function type
291 static const glsl_type
*get_function_instance(const struct glsl_type
*return_type
,
292 const glsl_function_param
*parameters
,
293 unsigned num_params
);
296 * Get the type resulting from a multiplication of \p type_a * \p type_b
298 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
299 const glsl_type
*type_b
);
302 * Query the total number of scalars that make up a scalar, vector or matrix
304 unsigned components() const
306 return vector_elements
* matrix_columns
;
310 * Calculate the number of components slots required to hold this type
312 * This is used to determine how many uniform or varying locations a type
315 unsigned component_slots() const;
318 * Calculate offset between the base location of the struct in
319 * uniform storage and a struct member.
320 * For the initial call, length is the index of the member to find the
323 unsigned record_location_offset(unsigned length
) const;
326 * Calculate the number of unique values from glGetUniformLocation for the
327 * elements of the type.
329 * This is used to allocate slots in the UniformRemapTable, the amount of
330 * locations may not match with actual used storage space by the driver.
332 unsigned uniform_locations() const;
335 * Used to count the number of varyings contained in the type ignoring
336 * innermost array elements.
338 unsigned varying_count() const;
341 * Calculate the number of attribute slots required to hold this type
343 * This implements the language rules of GLSL 1.50 for counting the number
344 * of slots used by a vertex attribute. It also determines the number of
345 * varying slots the type will use up in the absence of varying packing
346 * (and thus, it can be used to measure the number of varying slots used by
347 * the varyings that are generated by lower_packed_varyings).
349 * For vertex shader attributes - doubles only take one slot.
350 * For inter-shader varyings - dvec3/dvec4 take two slots.
352 unsigned count_attribute_slots(bool is_vertex_input
) const;
355 * Alignment in bytes of the start of this type in a std140 uniform
358 unsigned std140_base_alignment(bool row_major
) const;
360 /** Size in bytes of this type in a std140 uniform block.
362 * Note that this is not GL_UNIFORM_SIZE (which is the number of
363 * elements in the array)
365 unsigned std140_size(bool row_major
) const;
368 * Alignment in bytes of the start of this type in a std430 shader
371 unsigned std430_base_alignment(bool row_major
) const;
374 * Calculate array stride in bytes of this type in a std430 shader storage
377 unsigned std430_array_stride(bool row_major
) const;
380 * Size in bytes of this type in a std430 shader storage block.
382 * Note that this is not GL_BUFFER_SIZE
384 unsigned std430_size(bool row_major
) const;
387 * \brief Can this type be implicitly converted to another?
389 * \return True if the types are identical or if this type can be converted
390 * to \c desired according to Section 4.1.10 of the GLSL spec.
393 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
394 * Implicit Conversions:
396 * In some situations, an expression and its type will be implicitly
397 * converted to a different type. The following table shows all allowed
398 * implicit conversions:
400 * Type of expression | Can be implicitly converted to
401 * --------------------------------------------------
414 * There are no implicit array or structure conversions. For example,
415 * an array of int cannot be implicitly converted to an array of float.
416 * There are no implicit conversions between signed and unsigned
420 bool can_implicitly_convert_to(const glsl_type
*desired
,
421 _mesa_glsl_parse_state
*state
) const;
424 * Query whether or not a type is a scalar (non-vector and non-matrix).
426 bool is_scalar() const
428 return (vector_elements
== 1)
429 && (base_type
>= GLSL_TYPE_UINT
)
430 && (base_type
<= GLSL_TYPE_BOOL
);
434 * Query whether or not a type is a vector
436 bool is_vector() const
438 return (vector_elements
> 1)
439 && (matrix_columns
== 1)
440 && (base_type
>= GLSL_TYPE_UINT
)
441 && (base_type
<= GLSL_TYPE_BOOL
);
445 * Query whether or not a type is a matrix
447 bool is_matrix() const
449 /* GLSL only has float matrices. */
450 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
|| base_type
== GLSL_TYPE_DOUBLE
);
454 * Query whether or not a type is a non-array numeric type
456 bool is_numeric() const
458 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_DOUBLE
);
462 * Query whether or not a type is an integral type
464 bool is_integer() const
466 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
470 * Query whether or not type is an integral type, or for struct and array
471 * types, contains an integral type.
473 bool contains_integer() const;
476 * Query whether or not type is a double type, or for struct and array
477 * types, contains a double type.
479 bool contains_double() const;
482 * Query whether or not a type is a float type
484 bool is_float() const
486 return base_type
== GLSL_TYPE_FLOAT
;
490 * Query whether or not a type is a double type
492 bool is_double() const
494 return base_type
== GLSL_TYPE_DOUBLE
;
498 * Query whether a 64-bit type takes two slots.
500 bool is_dual_slot() const
502 return is_64bit() && vector_elements
> 2;
506 * Query whether or not a type is 64-bit
508 bool is_64bit() const
510 return glsl_base_type_is_64bit(base_type
);
514 * Query whether or not a type is a non-array boolean type
516 bool is_boolean() const
518 return base_type
== GLSL_TYPE_BOOL
;
522 * Query whether or not a type is a sampler
524 bool is_sampler() const
526 return base_type
== GLSL_TYPE_SAMPLER
;
530 * Query whether or not type is a sampler, or for struct and array
531 * types, contains a sampler.
533 bool contains_sampler() const;
536 * Get the Mesa texture target index for a sampler type.
538 gl_texture_index
sampler_index() const;
541 * Query whether or not type is an image, or for struct and array
542 * types, contains an image.
544 bool contains_image() const;
547 * Query whether or not a type is an image
549 bool is_image() const
551 return base_type
== GLSL_TYPE_IMAGE
;
555 * Query whether or not a type is an array
557 bool is_array() const
559 return base_type
== GLSL_TYPE_ARRAY
;
562 bool is_array_of_arrays() const
564 return is_array() && fields
.array
->is_array();
568 * Query whether or not a type is a record
570 bool is_record() const
572 return base_type
== GLSL_TYPE_STRUCT
;
576 * Query whether or not a type is an interface
578 bool is_interface() const
580 return base_type
== GLSL_TYPE_INTERFACE
;
584 * Query whether or not a type is the void type singleton.
588 return base_type
== GLSL_TYPE_VOID
;
592 * Query whether or not a type is the error type singleton.
594 bool is_error() const
596 return base_type
== GLSL_TYPE_ERROR
;
600 * Query if a type is unnamed/anonymous (named by the parser)
603 bool is_subroutine() const
605 return base_type
== GLSL_TYPE_SUBROUTINE
;
607 bool contains_subroutine() const;
609 bool is_anonymous() const
611 return !strncmp(name
, "#anon", 5);
615 * Get the type stripped of any arrays
618 * Pointer to the type of elements of the first non-array type for array
619 * types, or pointer to itself for non-array types.
621 const glsl_type
*without_array() const
623 const glsl_type
*t
= this;
625 while (t
->is_array())
632 * Return the total number of elements in an array including the elements
633 * in arrays of arrays.
635 unsigned arrays_of_arrays_size() const
640 unsigned size
= length
;
641 const glsl_type
*base_type
= fields
.array
;
643 while (base_type
->is_array()) {
644 size
= size
* base_type
->length
;
645 base_type
= base_type
->fields
.array
;
651 * Return the amount of atomic counter storage required for a type.
653 unsigned atomic_size() const
655 if (base_type
== GLSL_TYPE_ATOMIC_UINT
)
656 return ATOMIC_COUNTER_SIZE
;
658 return length
* fields
.array
->atomic_size();
664 * Return whether a type contains any atomic counters.
666 bool contains_atomic() const
668 return atomic_size() > 0;
672 * Return whether a type contains any opaque types.
674 bool contains_opaque() const;
677 * Query the full type of a matrix row
680 * If the type is not a matrix, \c glsl_type::error_type is returned.
681 * Otherwise a type matching the rows of the matrix is returned.
683 const glsl_type
*row_type() const
686 ? get_instance(base_type
, matrix_columns
, 1)
691 * Query the full type of a matrix column
694 * If the type is not a matrix, \c glsl_type::error_type is returned.
695 * Otherwise a type matching the columns of the matrix is returned.
697 const glsl_type
*column_type() const
700 ? get_instance(base_type
, vector_elements
, 1)
705 * Get the type of a structure field
708 * Pointer to the type of the named field. If the type is not a structure
709 * or the named field does not exist, \c glsl_type::error_type is returned.
711 const glsl_type
*field_type(const char *name
) const;
714 * Get the location of a field within a record type
716 int field_index(const char *name
) const;
719 * Query the number of elements in an array type
722 * The number of elements in the array for array types or -1 for non-array
723 * types. If the number of elements in the array has not yet been declared,
726 int array_size() const
728 return is_array() ? length
: -1;
732 * Query whether the array size for all dimensions has been declared.
734 bool is_unsized_array() const
736 return is_array() && length
== 0;
740 * Return the number of coordinate components needed for this
741 * sampler or image type.
743 * This is based purely on the sampler's dimensionality. For example, this
744 * returns 1 for sampler1D, and 3 for sampler2DArray.
746 * Note that this is often different than actual coordinate type used in
747 * a texturing built-in function, since those pack additional values (such
748 * as the shadow comparitor or projector) into the coordinate type.
750 int coordinate_components() const;
753 * Compare a record type against another record type.
755 * This is useful for matching record types declared across shader stages.
756 * The option to not match locations is to deal with places where the
757 * same struct is defined in a block which has a location set on it.
759 bool record_compare(const glsl_type
*b
, bool match_locations
= true) const;
762 * Get the type interface packing.
764 enum glsl_interface_packing
get_interface_packing() const
766 return (enum glsl_interface_packing
)interface_packing
;
774 * ralloc context for all glsl_type allocations
776 * Set on the first call to \c glsl_type::new.
778 static void *mem_ctx
;
780 void init_ralloc_type_ctx(void);
782 /** Constructor for vector and matrix types */
783 glsl_type(GLenum gl_type
,
784 glsl_base_type base_type
, unsigned vector_elements
,
785 unsigned matrix_columns
, const char *name
);
787 /** Constructor for sampler or image types */
788 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
789 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
790 unsigned type
, const char *name
);
792 /** Constructor for record types */
793 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
796 /** Constructor for interface types */
797 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
798 enum glsl_interface_packing packing
, const char *name
);
800 /** Constructor for interface types */
801 glsl_type(const glsl_type
*return_type
,
802 const glsl_function_param
*params
, unsigned num_params
);
804 /** Constructor for array types */
805 glsl_type(const glsl_type
*array
, unsigned length
);
807 /** Constructor for subroutine types */
808 glsl_type(const char *name
);
810 /** Hash table containing the known array types. */
811 static struct hash_table
*array_types
;
813 /** Hash table containing the known record types. */
814 static struct hash_table
*record_types
;
816 /** Hash table containing the known interface types. */
817 static struct hash_table
*interface_types
;
819 /** Hash table containing the known subroutine types. */
820 static struct hash_table
*subroutine_types
;
822 /** Hash table containing the known function types. */
823 static struct hash_table
*function_types
;
825 static bool record_key_compare(const void *a
, const void *b
);
826 static unsigned record_key_hash(const void *key
);
829 * \name Built-in type flyweights
833 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
835 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
836 #include "compiler/builtin_type_macros.h"
840 * \name Friend functions.
842 * These functions are friends because they must have C linkage and the
843 * need to call various private methods or access various private static
847 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
848 friend void _mesa_glsl_release_types(void);
854 #endif /* __cplusplus */
856 struct glsl_struct_field
{
857 const struct glsl_type
*type
;
861 * For interface blocks, gl_varying_slot corresponding to the input/output
862 * if this is a built-in input/output (i.e. a member of the built-in
863 * gl_PerVertex interface block); -1 otherwise.
865 * Ignored for structs.
870 * For interface blocks, members may have an explicit byte offset
871 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
873 * Unless used for xfb_offset this field is ignored for structs.
878 * For interface blocks, members may define a transform feedback buffer;
884 * For interface blocks, members may define a transform feedback stride;
890 * For interface blocks, the interpolation mode (as in
891 * ir_variable::interpolation). 0 otherwise.
893 unsigned interpolation
:2;
896 * For interface blocks, 1 if this variable uses centroid interpolation (as
897 * in ir_variable::centroid). 0 otherwise.
902 * For interface blocks, 1 if this variable uses sample interpolation (as
903 * in ir_variable::sample). 0 otherwise.
908 * Layout of the matrix. Uses glsl_matrix_layout values.
910 unsigned matrix_layout
:2;
913 * For interface blocks, 1 if this variable is a per-patch input or output
914 * (as in ir_variable::patch). 0 otherwise.
919 * Precision qualifier
921 unsigned precision
:2;
924 * Image qualifiers, applicable to buffer variables defined in shader
925 * storage buffer objects (SSBOs)
927 unsigned image_read_only
:1;
928 unsigned image_write_only
:1;
929 unsigned image_coherent
:1;
930 unsigned image_volatile
:1;
931 unsigned image_restrict
:1;
934 * Any of the xfb_* qualifiers trigger the shader to be in transform
935 * feedback mode so we need to keep track of whether the buffer was
936 * explicitly set or if its just been assigned the default global value.
938 unsigned explicit_xfb_buffer
:1;
940 unsigned implicit_sized_array
:1;
942 glsl_struct_field(const struct glsl_type
*_type
, const char *_name
)
943 : type(_type
), name(_name
), location(-1), interpolation(0), centroid(0),
944 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED
), patch(0),
945 precision(GLSL_PRECISION_NONE
), image_read_only(0), image_write_only(0),
946 image_coherent(0), image_volatile(0), image_restrict(0)
958 struct glsl_function_param
{
959 const struct glsl_type
*type
;
965 static inline unsigned int
966 glsl_align(unsigned int a
, unsigned int align
)
968 return (a
+ align
- 1) / align
* align
;
971 #endif /* GLSL_TYPES_H */