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
107 GLSL_PRECISION_NONE
= 0,
109 GLSL_PRECISION_MEDIUM
,
115 #include "util/ralloc.h"
116 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
120 glsl_base_type base_type
;
122 unsigned sampler_dimensionality
:3; /**< \see glsl_sampler_dim */
123 unsigned sampler_shadow
:1;
124 unsigned sampler_array
:1;
125 unsigned sampled_type
:2; /**< Type of data returned using this
126 * sampler or image. Only \c
127 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
128 * and \c GLSL_TYPE_UINT are valid.
130 unsigned interface_packing
:2;
132 /* Callers of this ralloc-based new need not call delete. It's
133 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
134 static void* operator new(size_t size
)
136 mtx_lock(&glsl_type::mutex
);
138 /* mem_ctx should have been created by the static members */
139 assert(glsl_type::mem_ctx
!= NULL
);
143 type
= ralloc_size(glsl_type::mem_ctx
, size
);
144 assert(type
!= NULL
);
146 mtx_unlock(&glsl_type::mutex
);
151 /* If the user *does* call delete, that's OK, we will just
152 * ralloc_free in that case. */
153 static void operator delete(void *type
)
155 mtx_lock(&glsl_type::mutex
);
157 mtx_unlock(&glsl_type::mutex
);
161 * \name Vector and matrix element counts
163 * For scalars, each of these values will be 1. For non-numeric types
167 uint8_t vector_elements
; /**< 1, 2, 3, or 4 vector elements. */
168 uint8_t matrix_columns
; /**< 1, 2, 3, or 4 matrix columns. */
172 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
173 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
174 * elements in the structure and the number of values pointed to by
175 * \c fields.structure (below).
180 * Name of the data type
182 * Will never be \c NULL.
187 * Subtype of composite data types.
190 const struct glsl_type
*array
; /**< Type of array elements. */
191 struct glsl_function_param
*parameters
; /**< Parameters to function. */
192 struct glsl_struct_field
*structure
; /**< List of struct fields. */
196 * \name Pointers to various public type singletons
200 #define DECL_TYPE(NAME, ...) \
201 static const glsl_type *const NAME##_type;
203 #define STRUCT_TYPE(NAME) \
204 static const glsl_type *const struct_##NAME##_type;
205 #include "compiler/builtin_type_macros.h"
209 * Convenience accessors for vector types (shorter than get_instance()).
212 static const glsl_type
*vec(unsigned components
);
213 static const glsl_type
*dvec(unsigned components
);
214 static const glsl_type
*ivec(unsigned components
);
215 static const glsl_type
*uvec(unsigned components
);
216 static const glsl_type
*bvec(unsigned components
);
220 * For numeric and boolean derived types returns the basic scalar type
222 * If the type is a numeric or boolean scalar, vector, or matrix type,
223 * this function gets the scalar type of the individual components. For
224 * all other types, including arrays of numeric or boolean types, the
225 * error type is returned.
227 const glsl_type
*get_base_type() const;
230 * Get the basic scalar type which this type aggregates.
232 * If the type is a numeric or boolean scalar, vector, or matrix, or an
233 * array of any of those, this function gets the scalar type of the
234 * individual components. For structs and arrays of structs, this function
235 * returns the struct type. For samplers and arrays of samplers, this
236 * function returns the sampler type.
238 const glsl_type
*get_scalar_type() const;
241 * Get the instance of a built-in scalar, vector, or matrix type
243 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
247 * Get the instance of a sampler type
249 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
252 glsl_base_type type
);
254 static const glsl_type
*get_image_instance(enum glsl_sampler_dim dim
,
255 bool array
, glsl_base_type type
);
258 * Get the instance of an array type
260 static const glsl_type
*get_array_instance(const glsl_type
*base
,
264 * Get the instance of a record type
266 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
271 * Get the instance of an interface block type
273 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
275 enum glsl_interface_packing packing
,
276 const char *block_name
);
279 * Get the instance of an subroutine type
281 static const glsl_type
*get_subroutine_instance(const char *subroutine_name
);
284 * Get the instance of a function type
286 static const glsl_type
*get_function_instance(const struct glsl_type
*return_type
,
287 const glsl_function_param
*parameters
,
288 unsigned num_params
);
291 * Get the type resulting from a multiplication of \p type_a * \p type_b
293 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
294 const glsl_type
*type_b
);
297 * Query the total number of scalars that make up a scalar, vector or matrix
299 unsigned components() const
301 return vector_elements
* matrix_columns
;
305 * Calculate the number of components slots required to hold this type
307 * This is used to determine how many uniform or varying locations a type
310 unsigned component_slots() const;
313 * Calculate offset between the base location of the struct in
314 * uniform storage and a struct member.
315 * For the initial call, length is the index of the member to find the
318 unsigned record_location_offset(unsigned length
) const;
321 * Calculate the number of unique values from glGetUniformLocation for the
322 * elements of the type.
324 * This is used to allocate slots in the UniformRemapTable, the amount of
325 * locations may not match with actual used storage space by the driver.
327 unsigned uniform_locations() const;
330 * Calculate the number of attribute slots required to hold this type
332 * This implements the language rules of GLSL 1.50 for counting the number
333 * of slots used by a vertex attribute. It also determines the number of
334 * varying slots the type will use up in the absence of varying packing
335 * (and thus, it can be used to measure the number of varying slots used by
336 * the varyings that are generated by lower_packed_varyings).
338 * For vertex shader attributes - doubles only take one slot.
339 * For inter-shader varyings - dvec3/dvec4 take two slots.
341 unsigned count_attribute_slots(bool vertex_input_slots
) const;
344 * Alignment in bytes of the start of this type in a std140 uniform
347 unsigned std140_base_alignment(bool row_major
) const;
349 /** Size in bytes of this type in a std140 uniform block.
351 * Note that this is not GL_UNIFORM_SIZE (which is the number of
352 * elements in the array)
354 unsigned std140_size(bool row_major
) const;
357 * Alignment in bytes of the start of this type in a std430 shader
360 unsigned std430_base_alignment(bool row_major
) const;
363 * Calculate array stride in bytes of this type in a std430 shader storage
366 unsigned std430_array_stride(bool row_major
) const;
369 * Size in bytes of this type in a std430 shader storage block.
371 * Note that this is not GL_BUFFER_SIZE
373 unsigned std430_size(bool row_major
) const;
376 * \brief Can this type be implicitly converted to another?
378 * \return True if the types are identical or if this type can be converted
379 * to \c desired according to Section 4.1.10 of the GLSL spec.
382 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
383 * Implicit Conversions:
385 * In some situations, an expression and its type will be implicitly
386 * converted to a different type. The following table shows all allowed
387 * implicit conversions:
389 * Type of expression | Can be implicitly converted to
390 * --------------------------------------------------
403 * There are no implicit array or structure conversions. For example,
404 * an array of int cannot be implicitly converted to an array of float.
405 * There are no implicit conversions between signed and unsigned
409 bool can_implicitly_convert_to(const glsl_type
*desired
,
410 _mesa_glsl_parse_state
*state
) const;
413 * Query whether or not a type is a scalar (non-vector and non-matrix).
415 bool is_scalar() const
417 return (vector_elements
== 1)
418 && (base_type
>= GLSL_TYPE_UINT
)
419 && (base_type
<= GLSL_TYPE_BOOL
);
423 * Query whether or not a type is a vector
425 bool is_vector() const
427 return (vector_elements
> 1)
428 && (matrix_columns
== 1)
429 && (base_type
>= GLSL_TYPE_UINT
)
430 && (base_type
<= GLSL_TYPE_BOOL
);
434 * Query whether or not a type is a matrix
436 bool is_matrix() const
438 /* GLSL only has float matrices. */
439 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
|| base_type
== GLSL_TYPE_DOUBLE
);
443 * Query whether or not a type is a non-array numeric type
445 bool is_numeric() const
447 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_DOUBLE
);
451 * Query whether or not a type is an integral type
453 bool is_integer() const
455 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
459 * Query whether or not type is an integral type, or for struct and array
460 * types, contains an integral type.
462 bool contains_integer() const;
465 * Query whether or not type is a double type, or for struct and array
466 * types, contains a double type.
468 bool contains_double() const;
471 * Query whether or not a type is a float type
473 bool is_float() const
475 return base_type
== GLSL_TYPE_FLOAT
;
479 * Query whether or not a type is a double type
481 bool is_double() const
483 return base_type
== GLSL_TYPE_DOUBLE
;
487 * Query whether a double takes two slots.
489 bool is_dual_slot_double() const
491 return base_type
== GLSL_TYPE_DOUBLE
&& vector_elements
> 2;
495 * Query whether or not a type is a non-array boolean type
497 bool is_boolean() const
499 return base_type
== GLSL_TYPE_BOOL
;
503 * Query whether or not a type is a sampler
505 bool is_sampler() const
507 return base_type
== GLSL_TYPE_SAMPLER
;
511 * Query whether or not type is a sampler, or for struct and array
512 * types, contains a sampler.
514 bool contains_sampler() const;
517 * Get the Mesa texture target index for a sampler type.
519 gl_texture_index
sampler_index() const;
522 * Query whether or not type is an image, or for struct and array
523 * types, contains an image.
525 bool contains_image() const;
528 * Query whether or not a type is an image
530 bool is_image() const
532 return base_type
== GLSL_TYPE_IMAGE
;
536 * Query whether or not a type is an array
538 bool is_array() const
540 return base_type
== GLSL_TYPE_ARRAY
;
543 bool is_array_of_arrays() const
545 return is_array() && fields
.array
->is_array();
549 * Query whether or not a type is a record
551 bool is_record() const
553 return base_type
== GLSL_TYPE_STRUCT
;
557 * Query whether or not a type is an interface
559 bool is_interface() const
561 return base_type
== GLSL_TYPE_INTERFACE
;
565 * Query whether or not a type is the void type singleton.
569 return base_type
== GLSL_TYPE_VOID
;
573 * Query whether or not a type is the error type singleton.
575 bool is_error() const
577 return base_type
== GLSL_TYPE_ERROR
;
581 * Query if a type is unnamed/anonymous (named by the parser)
584 bool is_subroutine() const
586 return base_type
== GLSL_TYPE_SUBROUTINE
;
588 bool contains_subroutine() const;
590 bool is_anonymous() const
592 return !strncmp(name
, "#anon", 5);
596 * Get the type stripped of any arrays
599 * Pointer to the type of elements of the first non-array type for array
600 * types, or pointer to itself for non-array types.
602 const glsl_type
*without_array() const
604 const glsl_type
*t
= this;
606 while (t
->is_array())
613 * Return the total number of elements in an array including the elements
614 * in arrays of arrays.
616 unsigned arrays_of_arrays_size() const
621 unsigned size
= length
;
622 const glsl_type
*base_type
= fields
.array
;
624 while (base_type
->is_array()) {
625 size
= size
* base_type
->length
;
626 base_type
= base_type
->fields
.array
;
632 * Return the amount of atomic counter storage required for a type.
634 unsigned atomic_size() const
636 if (base_type
== GLSL_TYPE_ATOMIC_UINT
)
637 return ATOMIC_COUNTER_SIZE
;
639 return length
* fields
.array
->atomic_size();
645 * Return whether a type contains any atomic counters.
647 bool contains_atomic() const
649 return atomic_size() > 0;
653 * Return whether a type contains any opaque types.
655 bool contains_opaque() const;
658 * Query the full type of a matrix row
661 * If the type is not a matrix, \c glsl_type::error_type is returned.
662 * Otherwise a type matching the rows of the matrix is returned.
664 const glsl_type
*row_type() const
667 ? get_instance(base_type
, matrix_columns
, 1)
672 * Query the full type of a matrix column
675 * If the type is not a matrix, \c glsl_type::error_type is returned.
676 * Otherwise a type matching the columns of the matrix is returned.
678 const glsl_type
*column_type() const
681 ? get_instance(base_type
, vector_elements
, 1)
686 * Get the type of a structure field
689 * Pointer to the type of the named field. If the type is not a structure
690 * or the named field does not exist, \c glsl_type::error_type is returned.
692 const glsl_type
*field_type(const char *name
) const;
695 * Get the location of a field within a record type
697 int field_index(const char *name
) const;
700 * Query the number of elements in an array type
703 * The number of elements in the array for array types or -1 for non-array
704 * types. If the number of elements in the array has not yet been declared,
707 int array_size() const
709 return is_array() ? length
: -1;
713 * Query whether the array size for all dimensions has been declared.
715 bool is_unsized_array() const
717 return is_array() && length
== 0;
721 * Return the number of coordinate components needed for this
722 * sampler or image type.
724 * This is based purely on the sampler's dimensionality. For example, this
725 * returns 1 for sampler1D, and 3 for sampler2DArray.
727 * Note that this is often different than actual coordinate type used in
728 * a texturing built-in function, since those pack additional values (such
729 * as the shadow comparitor or projector) into the coordinate type.
731 int coordinate_components() const;
734 * Compare a record type against another record type.
736 * This is useful for matching record types declared across shader stages.
738 bool record_compare(const glsl_type
*b
) const;
745 * ralloc context for all glsl_type allocations
747 * Set on the first call to \c glsl_type::new.
749 static void *mem_ctx
;
751 void init_ralloc_type_ctx(void);
753 /** Constructor for vector and matrix types */
754 glsl_type(GLenum gl_type
,
755 glsl_base_type base_type
, unsigned vector_elements
,
756 unsigned matrix_columns
, const char *name
);
758 /** Constructor for sampler or image types */
759 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
760 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
761 unsigned type
, const char *name
);
763 /** Constructor for record types */
764 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
767 /** Constructor for interface types */
768 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
769 enum glsl_interface_packing packing
, const char *name
);
771 /** Constructor for interface types */
772 glsl_type(const glsl_type
*return_type
,
773 const glsl_function_param
*params
, unsigned num_params
);
775 /** Constructor for array types */
776 glsl_type(const glsl_type
*array
, unsigned length
);
778 /** Constructor for subroutine types */
779 glsl_type(const char *name
);
781 /** Hash table containing the known array types. */
782 static struct hash_table
*array_types
;
784 /** Hash table containing the known record types. */
785 static struct hash_table
*record_types
;
787 /** Hash table containing the known interface types. */
788 static struct hash_table
*interface_types
;
790 /** Hash table containing the known subroutine types. */
791 static struct hash_table
*subroutine_types
;
793 /** Hash table containing the known function types. */
794 static struct hash_table
*function_types
;
796 static bool record_key_compare(const void *a
, const void *b
);
797 static unsigned record_key_hash(const void *key
);
800 * \name Built-in type flyweights
804 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
806 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
807 #include "compiler/builtin_type_macros.h"
811 * \name Friend functions.
813 * These functions are friends because they must have C linkage and the
814 * need to call various private methods or access various private static
818 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
819 friend void _mesa_glsl_release_types(void);
823 struct glsl_struct_field
{
824 const struct glsl_type
*type
;
828 * For interface blocks, gl_varying_slot corresponding to the input/output
829 * if this is a built-in input/output (i.e. a member of the built-in
830 * gl_PerVertex interface block); -1 otherwise.
832 * Ignored for structs.
837 * For interface blocks, the interpolation mode (as in
838 * ir_variable::interpolation). 0 otherwise.
840 unsigned interpolation
:2;
843 * For interface blocks, 1 if this variable uses centroid interpolation (as
844 * in ir_variable::centroid). 0 otherwise.
849 * For interface blocks, 1 if this variable uses sample interpolation (as
850 * in ir_variable::sample). 0 otherwise.
855 * Layout of the matrix. Uses glsl_matrix_layout values.
857 unsigned matrix_layout
:2;
860 * For interface blocks, 1 if this variable is a per-patch input or output
861 * (as in ir_variable::patch). 0 otherwise.
866 * Precision qualifier
868 unsigned precision
:2;
871 * Image qualifiers, applicable to buffer variables defined in shader
872 * storage buffer objects (SSBOs)
874 unsigned image_read_only
:1;
875 unsigned image_write_only
:1;
876 unsigned image_coherent
:1;
877 unsigned image_volatile
:1;
878 unsigned image_restrict
:1;
880 glsl_struct_field(const struct glsl_type
*_type
, const char *_name
)
881 : type(_type
), name(_name
), location(-1), interpolation(0), centroid(0),
882 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED
), patch(0),
883 precision(GLSL_PRECISION_NONE
), image_read_only(0), image_write_only(0),
884 image_coherent(0), image_volatile(0), image_restrict(0)
895 struct glsl_function_param
{
896 const struct glsl_type
*type
;
902 static inline unsigned int
903 glsl_align(unsigned int a
, unsigned int align
)
905 return (a
+ align
- 1) / align
* align
;
910 #endif /* __cplusplus */
912 #endif /* GLSL_TYPES_H */