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
,
66 enum glsl_sampler_dim
{
67 GLSL_SAMPLER_DIM_1D
= 0,
70 GLSL_SAMPLER_DIM_CUBE
,
71 GLSL_SAMPLER_DIM_RECT
,
73 GLSL_SAMPLER_DIM_EXTERNAL
,
77 enum glsl_interface_packing
{
78 GLSL_INTERFACE_PACKING_STD140
,
79 GLSL_INTERFACE_PACKING_SHARED
,
80 GLSL_INTERFACE_PACKING_PACKED
83 enum glsl_matrix_layout
{
85 * The layout of the matrix is inherited from the object containing the
86 * matrix (the top level structure or the uniform block).
88 GLSL_MATRIX_LAYOUT_INHERITED
,
91 * Explicit column-major layout
93 * If a uniform block doesn't have an explicit layout set, it will default
96 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
,
101 GLSL_MATRIX_LAYOUT_ROW_MAJOR
106 #include "util/ralloc.h"
107 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
111 glsl_base_type base_type
;
113 unsigned sampler_dimensionality
:3; /**< \see glsl_sampler_dim */
114 unsigned sampler_shadow
:1;
115 unsigned sampler_array
:1;
116 unsigned sampler_type
:2; /**< Type of data returned using this
117 * sampler or image. Only \c
118 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
119 * and \c GLSL_TYPE_UINT are valid.
121 unsigned interface_packing
:2;
123 /* Callers of this ralloc-based new need not call delete. It's
124 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
125 static void* operator new(size_t size
)
127 mtx_lock(&glsl_type::mutex
);
129 /* mem_ctx should have been created by the static members */
130 assert(glsl_type::mem_ctx
!= NULL
);
134 type
= ralloc_size(glsl_type::mem_ctx
, size
);
135 assert(type
!= NULL
);
137 mtx_unlock(&glsl_type::mutex
);
142 /* If the user *does* call delete, that's OK, we will just
143 * ralloc_free in that case. */
144 static void operator delete(void *type
)
146 mtx_lock(&glsl_type::mutex
);
148 mtx_unlock(&glsl_type::mutex
);
152 * \name Vector and matrix element counts
154 * For scalars, each of these values will be 1. For non-numeric types
158 uint8_t vector_elements
; /**< 1, 2, 3, or 4 vector elements. */
159 uint8_t matrix_columns
; /**< 1, 2, 3, or 4 matrix columns. */
163 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
164 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
165 * elements in the structure and the number of values pointed to by
166 * \c fields.structure (below).
171 * Name of the data type
173 * Will never be \c NULL.
178 * Subtype of composite data types.
181 const struct glsl_type
*array
; /**< Type of array elements. */
182 const struct glsl_type
*parameters
; /**< Parameters to function. */
183 struct glsl_struct_field
*structure
; /**< List of struct fields. */
187 * \name Pointers to various public type singletons
191 #define DECL_TYPE(NAME, ...) \
192 static const glsl_type *const NAME##_type;
194 #define STRUCT_TYPE(NAME) \
195 static const glsl_type *const struct_##NAME##_type;
196 #include "builtin_type_macros.h"
200 * Convenience accessors for vector types (shorter than get_instance()).
203 static const glsl_type
*vec(unsigned components
);
204 static const glsl_type
*dvec(unsigned components
);
205 static const glsl_type
*ivec(unsigned components
);
206 static const glsl_type
*uvec(unsigned components
);
207 static const glsl_type
*bvec(unsigned components
);
211 * For numeric and boolean derived types returns the basic scalar type
213 * If the type is a numeric or boolean scalar, vector, or matrix type,
214 * this function gets the scalar type of the individual components. For
215 * all other types, including arrays of numeric or boolean types, the
216 * error type is returned.
218 const glsl_type
*get_base_type() const;
221 * Get the basic scalar type which this type aggregates.
223 * If the type is a numeric or boolean scalar, vector, or matrix, or an
224 * array of any of those, this function gets the scalar type of the
225 * individual components. For structs and arrays of structs, this function
226 * returns the struct type. For samplers and arrays of samplers, this
227 * function returns the sampler type.
229 const glsl_type
*get_scalar_type() const;
232 * Get the instance of a built-in scalar, vector, or matrix type
234 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
238 * Get the instance of a sampler type
240 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
243 glsl_base_type type
);
247 * Get the instance of an array type
249 static const glsl_type
*get_array_instance(const glsl_type
*base
,
253 * Get the instance of a record type
255 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
260 * Get the instance of an interface block type
262 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
264 enum glsl_interface_packing packing
,
265 const char *block_name
);
268 * Get the instance of an subroutine type
270 static const glsl_type
*get_subroutine_instance(const char *subroutine_name
);
273 * Get the type resulting from a multiplication of \p type_a * \p type_b
275 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
276 const glsl_type
*type_b
);
279 * Query the total number of scalars that make up a scalar, vector or matrix
281 unsigned components() const
283 return vector_elements
* matrix_columns
;
287 * Calculate the number of components slots required to hold this type
289 * This is used to determine how many uniform or varying locations a type
292 unsigned component_slots() const;
295 * Calculate offset between the base location of the struct in
296 * uniform storage and a struct member.
297 * For the initial call, length is the index of the member to find the
300 unsigned record_location_offset(unsigned length
) const;
303 * Calculate the number of unique values from glGetUniformLocation for the
304 * elements of the type.
306 * This is used to allocate slots in the UniformRemapTable, the amount of
307 * locations may not match with actual used storage space by the driver.
309 unsigned uniform_locations() const;
312 * Calculate the number of attribute slots required to hold this type
314 * This implements the language rules of GLSL 1.50 for counting the number
315 * of slots used by a vertex attribute. It also determines the number of
316 * varying slots the type will use up in the absence of varying packing
317 * (and thus, it can be used to measure the number of varying slots used by
318 * the varyings that are generated by lower_packed_varyings).
320 unsigned count_attribute_slots() const;
324 * Alignment in bytes of the start of this type in a std140 uniform
327 unsigned std140_base_alignment(bool row_major
) const;
329 /** Size in bytes of this type in a std140 uniform block.
331 * Note that this is not GL_UNIFORM_SIZE (which is the number of
332 * elements in the array)
334 unsigned std140_size(bool row_major
) const;
337 * Alignment in bytes of the start of this type in a std430 shader
340 unsigned std430_base_alignment(bool row_major
) const;
343 * Calculate array stride in bytes of this type in a std430 shader storage
346 unsigned std430_array_stride(bool row_major
) const;
349 * Size in bytes of this type in a std430 shader storage block.
351 * Note that this is not GL_BUFFER_SIZE
353 unsigned std430_size(bool row_major
) const;
356 * \brief Can this type be implicitly converted to another?
358 * \return True if the types are identical or if this type can be converted
359 * to \c desired according to Section 4.1.10 of the GLSL spec.
362 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
363 * Implicit Conversions:
365 * In some situations, an expression and its type will be implicitly
366 * converted to a different type. The following table shows all allowed
367 * implicit conversions:
369 * Type of expression | Can be implicitly converted to
370 * --------------------------------------------------
383 * There are no implicit array or structure conversions. For example,
384 * an array of int cannot be implicitly converted to an array of float.
385 * There are no implicit conversions between signed and unsigned
389 bool can_implicitly_convert_to(const glsl_type
*desired
,
390 _mesa_glsl_parse_state
*state
) const;
393 * Query whether or not a type is a scalar (non-vector and non-matrix).
395 bool is_scalar() const
397 return (vector_elements
== 1)
398 && (base_type
>= GLSL_TYPE_UINT
)
399 && (base_type
<= GLSL_TYPE_BOOL
);
403 * Query whether or not a type is a vector
405 bool is_vector() const
407 return (vector_elements
> 1)
408 && (matrix_columns
== 1)
409 && (base_type
>= GLSL_TYPE_UINT
)
410 && (base_type
<= GLSL_TYPE_BOOL
);
414 * Query whether or not a type is a matrix
416 bool is_matrix() const
418 /* GLSL only has float matrices. */
419 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
|| base_type
== GLSL_TYPE_DOUBLE
);
423 * Query whether or not a type is a non-array numeric type
425 bool is_numeric() const
427 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_DOUBLE
);
431 * Query whether or not a type is an integral type
433 bool is_integer() const
435 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
439 * Query whether or not type is an integral type, or for struct and array
440 * types, contains an integral type.
442 bool contains_integer() const;
445 * Query whether or not type is a double type, or for struct and array
446 * types, contains a double type.
448 bool contains_double() const;
451 * Query whether or not a type is a float type
453 bool is_float() const
455 return base_type
== GLSL_TYPE_FLOAT
;
459 * Query whether or not a type is a double type
461 bool is_double() const
463 return base_type
== GLSL_TYPE_DOUBLE
;
467 * Query whether or not a type is a non-array boolean type
469 bool is_boolean() const
471 return base_type
== GLSL_TYPE_BOOL
;
475 * Query whether or not a type is a sampler
477 bool is_sampler() const
479 return base_type
== GLSL_TYPE_SAMPLER
;
483 * Query whether or not type is a sampler, or for struct and array
484 * types, contains a sampler.
486 bool contains_sampler() const;
489 * Get the Mesa texture target index for a sampler type.
491 gl_texture_index
sampler_index() const;
494 * Query whether or not type is an image, or for struct and array
495 * types, contains an image.
497 bool contains_image() const;
500 * Query whether or not a type is an image
502 bool is_image() const
504 return base_type
== GLSL_TYPE_IMAGE
;
508 * Query whether or not a type is an array
510 bool is_array() const
512 return base_type
== GLSL_TYPE_ARRAY
;
516 * Query whether or not a type is a record
518 bool is_record() const
520 return base_type
== GLSL_TYPE_STRUCT
;
524 * Query whether or not a type is an interface
526 bool is_interface() const
528 return base_type
== GLSL_TYPE_INTERFACE
;
532 * Query whether or not a type is the void type singleton.
536 return base_type
== GLSL_TYPE_VOID
;
540 * Query whether or not a type is the error type singleton.
542 bool is_error() const
544 return base_type
== GLSL_TYPE_ERROR
;
548 * Query if a type is unnamed/anonymous (named by the parser)
551 bool is_subroutine() const
553 return base_type
== GLSL_TYPE_SUBROUTINE
;
555 bool contains_subroutine() const;
557 bool is_anonymous() const
559 return !strncmp(name
, "#anon", 5);
563 * Get the type stripped of any arrays
566 * Pointer to the type of elements of the first non-array type for array
567 * types, or pointer to itself for non-array types.
569 const glsl_type
*without_array() const
571 const glsl_type
*t
= this;
573 while (t
->is_array())
580 * Return the amount of atomic counter storage required for a type.
582 unsigned atomic_size() const
584 if (base_type
== GLSL_TYPE_ATOMIC_UINT
)
585 return ATOMIC_COUNTER_SIZE
;
587 return length
* fields
.array
->atomic_size();
593 * Return whether a type contains any atomic counters.
595 bool contains_atomic() const
597 return atomic_size() > 0;
601 * Return whether a type contains any opaque types.
603 bool contains_opaque() const;
606 * Query the full type of a matrix row
609 * If the type is not a matrix, \c glsl_type::error_type is returned.
610 * Otherwise a type matching the rows of the matrix is returned.
612 const glsl_type
*row_type() const
615 ? get_instance(base_type
, matrix_columns
, 1)
620 * Query the full type of a matrix column
623 * If the type is not a matrix, \c glsl_type::error_type is returned.
624 * Otherwise a type matching the columns of the matrix is returned.
626 const glsl_type
*column_type() const
629 ? get_instance(base_type
, vector_elements
, 1)
634 * Get the type of a structure field
637 * Pointer to the type of the named field. If the type is not a structure
638 * or the named field does not exist, \c glsl_type::error_type is returned.
640 const glsl_type
*field_type(const char *name
) const;
643 * Get the location of a field within a record type
645 int field_index(const char *name
) const;
648 * Query the number of elements in an array type
651 * The number of elements in the array for array types or -1 for non-array
652 * types. If the number of elements in the array has not yet been declared,
655 int array_size() const
657 return is_array() ? length
: -1;
661 * Query whether the array size for all dimensions has been declared.
663 bool is_unsized_array() const
665 return is_array() && length
== 0;
669 * Return the number of coordinate components needed for this
670 * sampler or image type.
672 * This is based purely on the sampler's dimensionality. For example, this
673 * returns 1 for sampler1D, and 3 for sampler2DArray.
675 * Note that this is often different than actual coordinate type used in
676 * a texturing built-in function, since those pack additional values (such
677 * as the shadow comparitor or projector) into the coordinate type.
679 int coordinate_components() const;
682 * Compare a record type against another record type.
684 * This is useful for matching record types declared across shader stages.
686 bool record_compare(const glsl_type
*b
) const;
693 * ralloc context for all glsl_type allocations
695 * Set on the first call to \c glsl_type::new.
697 static void *mem_ctx
;
699 void init_ralloc_type_ctx(void);
701 /** Constructor for vector and matrix types */
702 glsl_type(GLenum gl_type
,
703 glsl_base_type base_type
, unsigned vector_elements
,
704 unsigned matrix_columns
, const char *name
);
706 /** Constructor for sampler or image types */
707 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
708 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
709 unsigned type
, const char *name
);
711 /** Constructor for record types */
712 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
715 /** Constructor for interface types */
716 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
717 enum glsl_interface_packing packing
, const char *name
);
719 /** Constructor for array types */
720 glsl_type(const glsl_type
*array
, unsigned length
);
722 /** Constructor for subroutine types */
723 glsl_type(const char *name
);
725 /** Hash table containing the known array types. */
726 static struct hash_table
*array_types
;
728 /** Hash table containing the known record types. */
729 static struct hash_table
*record_types
;
731 /** Hash table containing the known interface types. */
732 static struct hash_table
*interface_types
;
734 /** Hash table containing the known subroutine types. */
735 static struct hash_table
*subroutine_types
;
737 static bool record_key_compare(const void *a
, const void *b
);
738 static unsigned record_key_hash(const void *key
);
741 * \name Built-in type flyweights
745 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
747 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
748 #include "builtin_type_macros.h"
752 * \name Friend functions.
754 * These functions are friends because they must have C linkage and the
755 * need to call various private methods or access various private static
759 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
760 friend void _mesa_glsl_release_types(void);
764 struct glsl_struct_field
{
765 const struct glsl_type
*type
;
769 * For interface blocks, gl_varying_slot corresponding to the input/output
770 * if this is a built-in input/output (i.e. a member of the built-in
771 * gl_PerVertex interface block); -1 otherwise.
773 * Ignored for structs.
778 * For interface blocks, the interpolation mode (as in
779 * ir_variable::interpolation). 0 otherwise.
781 unsigned interpolation
:2;
784 * For interface blocks, 1 if this variable uses centroid interpolation (as
785 * in ir_variable::centroid). 0 otherwise.
790 * For interface blocks, 1 if this variable uses sample interpolation (as
791 * in ir_variable::sample). 0 otherwise.
796 * Layout of the matrix. Uses glsl_matrix_layout values.
798 unsigned matrix_layout
:2;
801 * For interface blocks, 1 if this variable is a per-patch input or output
802 * (as in ir_variable::patch). 0 otherwise.
807 * For interface blocks, it has a value if this variable uses multiple vertex
808 * streams (as in ir_variable::stream). -1 otherwise.
812 glsl_struct_field(const struct glsl_type
*_type
, const char *_name
)
813 : type(_type
), name(_name
), location(-1), interpolation(0), centroid(0),
814 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED
), patch(0),
826 static inline unsigned int
827 glsl_align(unsigned int a
, unsigned int align
)
829 return (a
+ align
- 1) / align
* align
;
834 #endif /* __cplusplus */
836 #endif /* GLSL_TYPES_H */