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
84 enum glsl_matrix_layout
{
86 * The layout of the matrix is inherited from the object containing the
87 * matrix (the top level structure or the uniform block).
89 GLSL_MATRIX_LAYOUT_INHERITED
,
92 * Explicit column-major layout
94 * If a uniform block doesn't have an explicit layout set, it will default
97 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
,
102 GLSL_MATRIX_LAYOUT_ROW_MAJOR
107 #include "util/ralloc.h"
108 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
112 glsl_base_type base_type
;
114 unsigned sampler_dimensionality
:3; /**< \see glsl_sampler_dim */
115 unsigned sampler_shadow
:1;
116 unsigned sampler_array
:1;
117 unsigned sampler_type
:2; /**< Type of data returned using this
118 * sampler or image. Only \c
119 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
120 * and \c GLSL_TYPE_UINT are valid.
122 unsigned interface_packing
:2;
124 /* Callers of this ralloc-based new need not call delete. It's
125 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
126 static void* operator new(size_t size
)
128 mtx_lock(&glsl_type::mutex
);
130 /* mem_ctx should have been created by the static members */
131 assert(glsl_type::mem_ctx
!= NULL
);
135 type
= ralloc_size(glsl_type::mem_ctx
, size
);
136 assert(type
!= NULL
);
138 mtx_unlock(&glsl_type::mutex
);
143 /* If the user *does* call delete, that's OK, we will just
144 * ralloc_free in that case. */
145 static void operator delete(void *type
)
147 mtx_lock(&glsl_type::mutex
);
149 mtx_unlock(&glsl_type::mutex
);
153 * \name Vector and matrix element counts
155 * For scalars, each of these values will be 1. For non-numeric types
159 uint8_t vector_elements
; /**< 1, 2, 3, or 4 vector elements. */
160 uint8_t matrix_columns
; /**< 1, 2, 3, or 4 matrix columns. */
164 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
165 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
166 * elements in the structure and the number of values pointed to by
167 * \c fields.structure (below).
172 * Name of the data type
174 * Will never be \c NULL.
179 * Subtype of composite data types.
182 const struct glsl_type
*array
; /**< Type of array elements. */
183 struct glsl_function_param
*parameters
; /**< Parameters to function. */
184 struct glsl_struct_field
*structure
; /**< List of struct fields. */
188 * \name Pointers to various public type singletons
192 #define DECL_TYPE(NAME, ...) \
193 static const glsl_type *const NAME##_type;
195 #define STRUCT_TYPE(NAME) \
196 static const glsl_type *const struct_##NAME##_type;
197 #include "builtin_type_macros.h"
201 * Convenience accessors for vector types (shorter than get_instance()).
204 static const glsl_type
*vec(unsigned components
);
205 static const glsl_type
*dvec(unsigned components
);
206 static const glsl_type
*ivec(unsigned components
);
207 static const glsl_type
*uvec(unsigned components
);
208 static const glsl_type
*bvec(unsigned components
);
212 * For numeric and boolean derived types returns the basic scalar type
214 * If the type is a numeric or boolean scalar, vector, or matrix type,
215 * this function gets the scalar type of the individual components. For
216 * all other types, including arrays of numeric or boolean types, the
217 * error type is returned.
219 const glsl_type
*get_base_type() const;
222 * Get the basic scalar type which this type aggregates.
224 * If the type is a numeric or boolean scalar, vector, or matrix, or an
225 * array of any of those, this function gets the scalar type of the
226 * individual components. For structs and arrays of structs, this function
227 * returns the struct type. For samplers and arrays of samplers, this
228 * function returns the sampler type.
230 const glsl_type
*get_scalar_type() const;
233 * Get the instance of a built-in scalar, vector, or matrix type
235 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
239 * Get the instance of a sampler type
241 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
244 glsl_base_type type
);
248 * Get the instance of an array type
250 static const glsl_type
*get_array_instance(const glsl_type
*base
,
254 * Get the instance of a record type
256 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
261 * Get the instance of an interface block type
263 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
265 enum glsl_interface_packing packing
,
266 const char *block_name
);
269 * Get the instance of a function type
271 static const glsl_type
*get_function_instance(const struct glsl_type
*return_type
,
272 const glsl_function_param
*parameters
,
273 unsigned num_params
);
275 * Get the instance of an subroutine type
277 static const glsl_type
*get_subroutine_instance(const char *subroutine_name
);
280 * Get the type resulting from a multiplication of \p type_a * \p type_b
282 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
283 const glsl_type
*type_b
);
286 * Query the total number of scalars that make up a scalar, vector or matrix
288 unsigned components() const
290 return vector_elements
* matrix_columns
;
294 * Calculate the number of components slots required to hold this type
296 * This is used to determine how many uniform or varying locations a type
299 unsigned component_slots() const;
302 * Calculate the number of unique values from glGetUniformLocation for the
303 * elements of the type.
305 * This is used to allocate slots in the UniformRemapTable, the amount of
306 * locations may not match with actual used storage space by the driver.
308 unsigned uniform_locations() const;
311 * Calculate the number of attribute slots required to hold this type
313 * This implements the language rules of GLSL 1.50 for counting the number
314 * of slots used by a vertex attribute. It also determines the number of
315 * varying slots the type will use up in the absence of varying packing
316 * (and thus, it can be used to measure the number of varying slots used by
317 * the varyings that are generated by lower_packed_varyings).
319 unsigned count_attribute_slots() const;
323 * Alignment in bytes of the start of this type in a std140 uniform
326 unsigned std140_base_alignment(bool row_major
) const;
328 /** Size in bytes of this type in a std140 uniform block.
330 * Note that this is not GL_UNIFORM_SIZE (which is the number of
331 * elements in the array)
333 unsigned std140_size(bool row_major
) const;
336 * \brief Can this type be implicitly converted to another?
338 * \return True if the types are identical or if this type can be converted
339 * to \c desired according to Section 4.1.10 of the GLSL spec.
342 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
343 * Implicit Conversions:
345 * In some situations, an expression and its type will be implicitly
346 * converted to a different type. The following table shows all allowed
347 * implicit conversions:
349 * Type of expression | Can be implicitly converted to
350 * --------------------------------------------------
363 * There are no implicit array or structure conversions. For example,
364 * an array of int cannot be implicitly converted to an array of float.
365 * There are no implicit conversions between signed and unsigned
369 bool can_implicitly_convert_to(const glsl_type
*desired
,
370 _mesa_glsl_parse_state
*state
) const;
373 * Query whether or not a type is a scalar (non-vector and non-matrix).
375 bool is_scalar() const
377 return (vector_elements
== 1)
378 && (base_type
>= GLSL_TYPE_UINT
)
379 && (base_type
<= GLSL_TYPE_BOOL
);
383 * Query whether or not a type is a vector
385 bool is_vector() const
387 return (vector_elements
> 1)
388 && (matrix_columns
== 1)
389 && (base_type
>= GLSL_TYPE_UINT
)
390 && (base_type
<= GLSL_TYPE_BOOL
);
394 * Query whether or not a type is a matrix
396 bool is_matrix() const
398 /* GLSL only has float matrices. */
399 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
|| base_type
== GLSL_TYPE_DOUBLE
);
403 * Query whether or not a type is a non-array numeric type
405 bool is_numeric() const
407 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_DOUBLE
);
411 * Query whether or not a type is an integral type
413 bool is_integer() const
415 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
419 * Query whether or not type is an integral type, or for struct and array
420 * types, contains an integral type.
422 bool contains_integer() const;
425 * Query whether or not type is a double type, or for struct and array
426 * types, contains a double type.
428 bool contains_double() const;
431 * Query whether or not a type is a float type
433 bool is_float() const
435 return base_type
== GLSL_TYPE_FLOAT
;
439 * Query whether or not a type is a double type
441 bool is_double() const
443 return base_type
== GLSL_TYPE_DOUBLE
;
447 * Query whether or not a type is a non-array boolean type
449 bool is_boolean() const
451 return base_type
== GLSL_TYPE_BOOL
;
455 * Query whether or not a type is a sampler
457 bool is_sampler() const
459 return base_type
== GLSL_TYPE_SAMPLER
;
463 * Query whether or not type is a sampler, or for struct and array
464 * types, contains a sampler.
466 bool contains_sampler() const;
469 * Get the Mesa texture target index for a sampler type.
471 gl_texture_index
sampler_index() const;
474 * Query whether or not type is an image, or for struct and array
475 * types, contains an image.
477 bool contains_image() const;
480 * Query whether or not a type is an image
482 bool is_image() const
484 return base_type
== GLSL_TYPE_IMAGE
;
488 * Query whether or not a type is an array
490 bool is_array() const
492 return base_type
== GLSL_TYPE_ARRAY
;
496 * Query whether or not a type is a record
498 bool is_record() const
500 return base_type
== GLSL_TYPE_STRUCT
;
504 * Query whether or not a type is an interface
506 bool is_interface() const
508 return base_type
== GLSL_TYPE_INTERFACE
;
512 * Query whether or not a type is the void type singleton.
516 return base_type
== GLSL_TYPE_VOID
;
520 * Query whether or not a type is the error type singleton.
522 bool is_error() const
524 return base_type
== GLSL_TYPE_ERROR
;
528 * Query if a type is unnamed/anonymous (named by the parser)
531 bool is_subroutine() const
533 return base_type
== GLSL_TYPE_SUBROUTINE
;
535 bool contains_subroutine() const;
537 bool is_anonymous() const
539 return !strncmp(name
, "#anon", 5);
543 * Get the type stripped of any arrays
546 * Pointer to the type of elements of the first non-array type for array
547 * types, or pointer to itself for non-array types.
549 const glsl_type
*without_array() const
551 const glsl_type
*t
= this;
553 while (t
->is_array())
560 * Return the amount of atomic counter storage required for a type.
562 unsigned atomic_size() const
564 if (base_type
== GLSL_TYPE_ATOMIC_UINT
)
565 return ATOMIC_COUNTER_SIZE
;
567 return length
* fields
.array
->atomic_size();
573 * Return whether a type contains any atomic counters.
575 bool contains_atomic() const
577 return atomic_size() > 0;
581 * Return whether a type contains any opaque types.
583 bool contains_opaque() const;
586 * Query the full type of a matrix row
589 * If the type is not a matrix, \c glsl_type::error_type is returned.
590 * Otherwise a type matching the rows of the matrix is returned.
592 const glsl_type
*row_type() const
595 ? get_instance(base_type
, matrix_columns
, 1)
600 * Query the full type of a matrix column
603 * If the type is not a matrix, \c glsl_type::error_type is returned.
604 * Otherwise a type matching the columns of the matrix is returned.
606 const glsl_type
*column_type() const
609 ? get_instance(base_type
, vector_elements
, 1)
614 * Get the type of a structure field
617 * Pointer to the type of the named field. If the type is not a structure
618 * or the named field does not exist, \c glsl_type::error_type is returned.
620 const glsl_type
*field_type(const char *name
) const;
623 * Get the location of a filed within a record type
625 int field_index(const char *name
) const;
628 * Query the number of elements in an array type
631 * The number of elements in the array for array types or -1 for non-array
632 * types. If the number of elements in the array has not yet been declared,
635 int array_size() const
637 return is_array() ? length
: -1;
641 * Query whether the array size for all dimensions has been declared.
643 bool is_unsized_array() const
645 return is_array() && length
== 0;
649 * Return the number of coordinate components needed for this
650 * sampler or image type.
652 * This is based purely on the sampler's dimensionality. For example, this
653 * returns 1 for sampler1D, and 3 for sampler2DArray.
655 * Note that this is often different than actual coordinate type used in
656 * a texturing built-in function, since those pack additional values (such
657 * as the shadow comparitor or projector) into the coordinate type.
659 int coordinate_components() const;
662 * Compare a record type against another record type.
664 * This is useful for matching record types declared across shader stages.
666 bool record_compare(const glsl_type
*b
) const;
673 * ralloc context for all glsl_type allocations
675 * Set on the first call to \c glsl_type::new.
677 static void *mem_ctx
;
679 void init_ralloc_type_ctx(void);
681 /** Constructor for vector and matrix types */
682 glsl_type(GLenum gl_type
,
683 glsl_base_type base_type
, unsigned vector_elements
,
684 unsigned matrix_columns
, const char *name
);
686 /** Constructor for sampler or image types */
687 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
688 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
689 unsigned type
, const char *name
);
691 /** Constructor for record types */
692 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
695 /** Constructor for interface types */
696 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
697 enum glsl_interface_packing packing
, const char *name
);
699 /** Constructor for interface types */
700 glsl_type(const glsl_type
*return_type
,
701 const glsl_function_param
*params
, unsigned num_params
);
703 /** Constructor for array types */
704 glsl_type(const glsl_type
*array
, unsigned length
);
706 /** Constructor for subroutine types */
707 glsl_type(const char *name
);
709 /** Hash table containing the known array types. */
710 static struct hash_table
*array_types
;
712 /** Hash table containing the known record types. */
713 static struct hash_table
*record_types
;
715 /** Hash table containing the known interface types. */
716 static struct hash_table
*interface_types
;
718 /** Hash table containing the known function types. */
719 static struct hash_table
*function_types
;
721 /** Hash table containing the known subroutine types. */
722 static struct hash_table
*subroutine_types
;
724 static bool record_key_compare(const void *a
, const void *b
);
725 static unsigned record_key_hash(const void *key
);
728 * \name Built-in type flyweights
732 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
734 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
735 #include "builtin_type_macros.h"
739 * \name Friend functions.
741 * These functions are friends because they must have C linkage and the
742 * need to call various private methods or access various private static
746 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
747 friend void _mesa_glsl_release_types(void);
753 #endif /* __cplusplus */
755 struct glsl_struct_field
{
756 const struct glsl_type
*type
;
760 * For interface blocks, gl_varying_slot corresponding to the input/output
761 * if this is a built-in input/output (i.e. a member of the built-in
762 * gl_PerVertex interface block); -1 otherwise.
764 * Ignored for structs.
769 * For interface blocks, the interpolation mode (as in
770 * ir_variable::interpolation). 0 otherwise.
772 unsigned interpolation
:2;
775 * For interface blocks, 1 if this variable uses centroid interpolation (as
776 * in ir_variable::centroid). 0 otherwise.
781 * For interface blocks, 1 if this variable uses sample interpolation (as
782 * in ir_variable::sample). 0 otherwise.
787 * Layout of the matrix. Uses glsl_matrix_layout values.
789 unsigned matrix_layout
:2;
792 * For interface blocks, 1 if this variable is a per-patch input or output
793 * (as in ir_variable::patch). 0 otherwise.
798 * For interface blocks, it has a value if this variable uses multiple vertex
799 * streams (as in ir_variable::stream). -1 otherwise.
804 glsl_struct_field(const struct glsl_type
*_type
, const char *_name
)
805 : type(_type
), name(_name
), location(-1), interpolation(0), centroid(0),
806 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED
), patch(0),
819 struct glsl_function_param
{
820 const struct glsl_type
*type
;
826 static inline unsigned int
827 glsl_align(unsigned int a
, unsigned int align
)
829 return (a
+ align
- 1) / align
* align
;
832 #endif /* GLSL_TYPES_H */