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 enum glsl_sampler_dim
{
68 GLSL_SAMPLER_DIM_1D
= 0,
71 GLSL_SAMPLER_DIM_CUBE
,
72 GLSL_SAMPLER_DIM_RECT
,
74 GLSL_SAMPLER_DIM_EXTERNAL
,
78 enum glsl_interface_packing
{
79 GLSL_INTERFACE_PACKING_STD140
,
80 GLSL_INTERFACE_PACKING_SHARED
,
81 GLSL_INTERFACE_PACKING_PACKED
,
82 GLSL_INTERFACE_PACKING_STD430
85 enum glsl_matrix_layout
{
87 * The layout of the matrix is inherited from the object containing the
88 * matrix (the top level structure or the uniform block).
90 GLSL_MATRIX_LAYOUT_INHERITED
,
93 * Explicit column-major layout
95 * If a uniform block doesn't have an explicit layout set, it will default
98 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
,
103 GLSL_MATRIX_LAYOUT_ROW_MAJOR
108 #include "util/ralloc.h"
109 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
113 glsl_base_type base_type
;
115 unsigned sampler_dimensionality
:3; /**< \see glsl_sampler_dim */
116 unsigned sampler_shadow
:1;
117 unsigned sampler_array
:1;
118 unsigned sampler_type
:2; /**< Type of data returned using this
119 * sampler or image. Only \c
120 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
121 * and \c GLSL_TYPE_UINT are valid.
123 unsigned interface_packing
:2;
125 /* Callers of this ralloc-based new need not call delete. It's
126 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
127 static void* operator new(size_t size
)
129 mtx_lock(&glsl_type::mutex
);
131 /* mem_ctx should have been created by the static members */
132 assert(glsl_type::mem_ctx
!= NULL
);
136 type
= ralloc_size(glsl_type::mem_ctx
, size
);
137 assert(type
!= NULL
);
139 mtx_unlock(&glsl_type::mutex
);
144 /* If the user *does* call delete, that's OK, we will just
145 * ralloc_free in that case. */
146 static void operator delete(void *type
)
148 mtx_lock(&glsl_type::mutex
);
150 mtx_unlock(&glsl_type::mutex
);
154 * \name Vector and matrix element counts
156 * For scalars, each of these values will be 1. For non-numeric types
160 uint8_t vector_elements
; /**< 1, 2, 3, or 4 vector elements. */
161 uint8_t matrix_columns
; /**< 1, 2, 3, or 4 matrix columns. */
165 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
166 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
167 * elements in the structure and the number of values pointed to by
168 * \c fields.structure (below).
173 * Name of the data type
175 * Will never be \c NULL.
180 * Subtype of composite data types.
183 const struct glsl_type
*array
; /**< Type of array elements. */
184 struct glsl_function_param
*parameters
; /**< Parameters to function. */
185 struct glsl_struct_field
*structure
; /**< List of struct fields. */
189 * \name Pointers to various public type singletons
193 #define DECL_TYPE(NAME, ...) \
194 static const glsl_type *const NAME##_type;
196 #define STRUCT_TYPE(NAME) \
197 static const glsl_type *const struct_##NAME##_type;
198 #include "builtin_type_macros.h"
202 * Convenience accessors for vector types (shorter than get_instance()).
205 static const glsl_type
*vec(unsigned components
);
206 static const glsl_type
*dvec(unsigned components
);
207 static const glsl_type
*ivec(unsigned components
);
208 static const glsl_type
*uvec(unsigned components
);
209 static const glsl_type
*bvec(unsigned components
);
213 * For numeric and boolean derived types returns the basic scalar type
215 * If the type is a numeric or boolean scalar, vector, or matrix type,
216 * this function gets the scalar type of the individual components. For
217 * all other types, including arrays of numeric or boolean types, the
218 * error type is returned.
220 const glsl_type
*get_base_type() const;
223 * Get the basic scalar type which this type aggregates.
225 * If the type is a numeric or boolean scalar, vector, or matrix, or an
226 * array of any of those, this function gets the scalar type of the
227 * individual components. For structs and arrays of structs, this function
228 * returns the struct type. For samplers and arrays of samplers, this
229 * function returns the sampler type.
231 const glsl_type
*get_scalar_type() const;
234 * Get the instance of a built-in scalar, vector, or matrix type
236 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
240 * Get the instance of a sampler type
242 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
245 glsl_base_type type
);
249 * Get the instance of an array type
251 static const glsl_type
*get_array_instance(const glsl_type
*base
,
255 * Get the instance of a record type
257 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
262 * Get the instance of an interface block type
264 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
266 enum glsl_interface_packing packing
,
267 const char *block_name
);
270 * Get the instance of an subroutine type
272 static const glsl_type
*get_subroutine_instance(const char *subroutine_name
);
275 * Get the instance of a function type
277 static const glsl_type
*get_function_instance(const struct glsl_type
*return_type
,
278 const glsl_function_param
*parameters
,
279 unsigned num_params
);
282 * Get the type resulting from a multiplication of \p type_a * \p type_b
284 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
285 const glsl_type
*type_b
);
288 * Query the total number of scalars that make up a scalar, vector or matrix
290 unsigned components() const
292 return vector_elements
* matrix_columns
;
296 * Calculate the number of components slots required to hold this type
298 * This is used to determine how many uniform or varying locations a type
301 unsigned component_slots() const;
304 * Calculate offset between the base location of the struct in
305 * uniform storage and a struct member.
306 * For the initial call, length is the index of the member to find the
309 unsigned record_location_offset(unsigned length
) const;
312 * Calculate the number of unique values from glGetUniformLocation for the
313 * elements of the type.
315 * This is used to allocate slots in the UniformRemapTable, the amount of
316 * locations may not match with actual used storage space by the driver.
318 unsigned uniform_locations() const;
321 * Calculate the number of attribute slots required to hold this type
323 * This implements the language rules of GLSL 1.50 for counting the number
324 * of slots used by a vertex attribute. It also determines the number of
325 * varying slots the type will use up in the absence of varying packing
326 * (and thus, it can be used to measure the number of varying slots used by
327 * the varyings that are generated by lower_packed_varyings).
329 unsigned count_attribute_slots() const;
333 * Alignment in bytes of the start of this type in a std140 uniform
336 unsigned std140_base_alignment(bool row_major
) const;
338 /** Size in bytes of this type in a std140 uniform block.
340 * Note that this is not GL_UNIFORM_SIZE (which is the number of
341 * elements in the array)
343 unsigned std140_size(bool row_major
) const;
346 * Alignment in bytes of the start of this type in a std430 shader
349 unsigned std430_base_alignment(bool row_major
) const;
352 * Calculate array stride in bytes of this type in a std430 shader storage
355 unsigned std430_array_stride(bool row_major
) const;
358 * Size in bytes of this type in a std430 shader storage block.
360 * Note that this is not GL_BUFFER_SIZE
362 unsigned std430_size(bool row_major
) const;
365 * \brief Can this type be implicitly converted to another?
367 * \return True if the types are identical or if this type can be converted
368 * to \c desired according to Section 4.1.10 of the GLSL spec.
371 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
372 * Implicit Conversions:
374 * In some situations, an expression and its type will be implicitly
375 * converted to a different type. The following table shows all allowed
376 * implicit conversions:
378 * Type of expression | Can be implicitly converted to
379 * --------------------------------------------------
392 * There are no implicit array or structure conversions. For example,
393 * an array of int cannot be implicitly converted to an array of float.
394 * There are no implicit conversions between signed and unsigned
398 bool can_implicitly_convert_to(const glsl_type
*desired
,
399 _mesa_glsl_parse_state
*state
) const;
402 * Query whether or not a type is a scalar (non-vector and non-matrix).
404 bool is_scalar() const
406 return (vector_elements
== 1)
407 && (base_type
>= GLSL_TYPE_UINT
)
408 && (base_type
<= GLSL_TYPE_BOOL
);
412 * Query whether or not a type is a vector
414 bool is_vector() const
416 return (vector_elements
> 1)
417 && (matrix_columns
== 1)
418 && (base_type
>= GLSL_TYPE_UINT
)
419 && (base_type
<= GLSL_TYPE_BOOL
);
423 * Query whether or not a type is a matrix
425 bool is_matrix() const
427 /* GLSL only has float matrices. */
428 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
|| base_type
== GLSL_TYPE_DOUBLE
);
432 * Query whether or not a type is a non-array numeric type
434 bool is_numeric() const
436 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_DOUBLE
);
440 * Query whether or not a type is an integral type
442 bool is_integer() const
444 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
448 * Query whether or not type is an integral type, or for struct and array
449 * types, contains an integral type.
451 bool contains_integer() const;
454 * Query whether or not type is a double type, or for struct and array
455 * types, contains a double type.
457 bool contains_double() const;
460 * Query whether or not a type is a float type
462 bool is_float() const
464 return base_type
== GLSL_TYPE_FLOAT
;
468 * Query whether or not a type is a double type
470 bool is_double() const
472 return base_type
== GLSL_TYPE_DOUBLE
;
476 * Query whether or not a type is a non-array boolean type
478 bool is_boolean() const
480 return base_type
== GLSL_TYPE_BOOL
;
484 * Query whether or not a type is a sampler
486 bool is_sampler() const
488 return base_type
== GLSL_TYPE_SAMPLER
;
492 * Query whether or not type is a sampler, or for struct and array
493 * types, contains a sampler.
495 bool contains_sampler() const;
498 * Get the Mesa texture target index for a sampler type.
500 gl_texture_index
sampler_index() const;
503 * Query whether or not type is an image, or for struct and array
504 * types, contains an image.
506 bool contains_image() const;
509 * Query whether or not a type is an image
511 bool is_image() const
513 return base_type
== GLSL_TYPE_IMAGE
;
517 * Query whether or not a type is an array
519 bool is_array() const
521 return base_type
== GLSL_TYPE_ARRAY
;
525 * Query whether or not a type is a record
527 bool is_record() const
529 return base_type
== GLSL_TYPE_STRUCT
;
533 * Query whether or not a type is an interface
535 bool is_interface() const
537 return base_type
== GLSL_TYPE_INTERFACE
;
541 * Query whether or not a type is the void type singleton.
545 return base_type
== GLSL_TYPE_VOID
;
549 * Query whether or not a type is the error type singleton.
551 bool is_error() const
553 return base_type
== GLSL_TYPE_ERROR
;
557 * Query if a type is unnamed/anonymous (named by the parser)
560 bool is_subroutine() const
562 return base_type
== GLSL_TYPE_SUBROUTINE
;
564 bool contains_subroutine() const;
566 bool is_anonymous() const
568 return !strncmp(name
, "#anon", 5);
572 * Get the type stripped of any arrays
575 * Pointer to the type of elements of the first non-array type for array
576 * types, or pointer to itself for non-array types.
578 const glsl_type
*without_array() const
580 const glsl_type
*t
= this;
582 while (t
->is_array())
589 * Return the total number of elements in an array including the elements
590 * in arrays of arrays.
592 unsigned arrays_of_arrays_size() const
597 unsigned size
= length
;
598 const glsl_type
*base_type
= fields
.array
;
600 while (base_type
->is_array()) {
601 size
= size
* base_type
->length
;
602 base_type
= base_type
->fields
.array
;
608 * Return the amount of atomic counter storage required for a type.
610 unsigned atomic_size() const
612 if (base_type
== GLSL_TYPE_ATOMIC_UINT
)
613 return ATOMIC_COUNTER_SIZE
;
615 return length
* fields
.array
->atomic_size();
621 * Return whether a type contains any atomic counters.
623 bool contains_atomic() const
625 return atomic_size() > 0;
629 * Return whether a type contains any opaque types.
631 bool contains_opaque() const;
634 * Query the full type of a matrix row
637 * If the type is not a matrix, \c glsl_type::error_type is returned.
638 * Otherwise a type matching the rows of the matrix is returned.
640 const glsl_type
*row_type() const
643 ? get_instance(base_type
, matrix_columns
, 1)
648 * Query the full type of a matrix column
651 * If the type is not a matrix, \c glsl_type::error_type is returned.
652 * Otherwise a type matching the columns of the matrix is returned.
654 const glsl_type
*column_type() const
657 ? get_instance(base_type
, vector_elements
, 1)
662 * Get the type of a structure field
665 * Pointer to the type of the named field. If the type is not a structure
666 * or the named field does not exist, \c glsl_type::error_type is returned.
668 const glsl_type
*field_type(const char *name
) const;
671 * Get the location of a field within a record type
673 int field_index(const char *name
) const;
676 * Query the number of elements in an array type
679 * The number of elements in the array for array types or -1 for non-array
680 * types. If the number of elements in the array has not yet been declared,
683 int array_size() const
685 return is_array() ? length
: -1;
689 * Query whether the array size for all dimensions has been declared.
691 bool is_unsized_array() const
693 return is_array() && length
== 0;
697 * Return the number of coordinate components needed for this
698 * sampler or image type.
700 * This is based purely on the sampler's dimensionality. For example, this
701 * returns 1 for sampler1D, and 3 for sampler2DArray.
703 * Note that this is often different than actual coordinate type used in
704 * a texturing built-in function, since those pack additional values (such
705 * as the shadow comparitor or projector) into the coordinate type.
707 int coordinate_components() const;
710 * Compare a record type against another record type.
712 * This is useful for matching record types declared across shader stages.
714 bool record_compare(const glsl_type
*b
) const;
721 * ralloc context for all glsl_type allocations
723 * Set on the first call to \c glsl_type::new.
725 static void *mem_ctx
;
727 void init_ralloc_type_ctx(void);
729 /** Constructor for vector and matrix types */
730 glsl_type(GLenum gl_type
,
731 glsl_base_type base_type
, unsigned vector_elements
,
732 unsigned matrix_columns
, const char *name
);
734 /** Constructor for sampler or image types */
735 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
736 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
737 unsigned type
, const char *name
);
739 /** Constructor for record types */
740 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
743 /** Constructor for interface types */
744 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
745 enum glsl_interface_packing packing
, const char *name
);
747 /** Constructor for interface types */
748 glsl_type(const glsl_type
*return_type
,
749 const glsl_function_param
*params
, unsigned num_params
);
751 /** Constructor for array types */
752 glsl_type(const glsl_type
*array
, unsigned length
);
754 /** Constructor for subroutine types */
755 glsl_type(const char *name
);
757 /** Hash table containing the known array types. */
758 static struct hash_table
*array_types
;
760 /** Hash table containing the known record types. */
761 static struct hash_table
*record_types
;
763 /** Hash table containing the known interface types. */
764 static struct hash_table
*interface_types
;
766 /** Hash table containing the known subroutine types. */
767 static struct hash_table
*subroutine_types
;
769 /** Hash table containing the known function types. */
770 static struct hash_table
*function_types
;
772 static bool record_key_compare(const void *a
, const void *b
);
773 static unsigned record_key_hash(const void *key
);
776 * \name Built-in type flyweights
780 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
782 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
783 #include "builtin_type_macros.h"
787 * \name Friend functions.
789 * These functions are friends because they must have C linkage and the
790 * need to call various private methods or access various private static
794 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
795 friend void _mesa_glsl_release_types(void);
801 #endif /* __cplusplus */
803 struct glsl_struct_field
{
804 const struct glsl_type
*type
;
808 * For interface blocks, gl_varying_slot corresponding to the input/output
809 * if this is a built-in input/output (i.e. a member of the built-in
810 * gl_PerVertex interface block); -1 otherwise.
812 * Ignored for structs.
817 * For interface blocks, the interpolation mode (as in
818 * ir_variable::interpolation). 0 otherwise.
820 unsigned interpolation
:2;
823 * For interface blocks, 1 if this variable uses centroid interpolation (as
824 * in ir_variable::centroid). 0 otherwise.
829 * For interface blocks, 1 if this variable uses sample interpolation (as
830 * in ir_variable::sample). 0 otherwise.
835 * Layout of the matrix. Uses glsl_matrix_layout values.
837 unsigned matrix_layout
:2;
840 * For interface blocks, 1 if this variable is a per-patch input or output
841 * (as in ir_variable::patch). 0 otherwise.
846 * For interface blocks, it has a value if this variable uses multiple vertex
847 * streams (as in ir_variable::stream). -1 otherwise.
852 * Image qualifiers, applicable to buffer variables defined in shader
853 * storage buffer objects (SSBOs)
855 unsigned image_read_only
:1;
856 unsigned image_write_only
:1;
857 unsigned image_coherent
:1;
858 unsigned image_volatile
:1;
859 unsigned image_restrict
:1;
862 glsl_struct_field(const struct glsl_type
*_type
, const char *_name
)
863 : type(_type
), name(_name
), location(-1), interpolation(0), centroid(0),
864 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED
), patch(0),
877 struct glsl_function_param
{
878 const struct glsl_type
*type
;
884 static inline unsigned int
885 glsl_align(unsigned int a
, unsigned int align
)
887 return (a
+ align
- 1) / align
* align
;
890 #endif /* GLSL_TYPES_H */