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,
81 GLSL_INTERFACE_PACKING_STD430
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
106 GLSL_PRECISION_NONE = 0,
108 GLSL_PRECISION_MEDIUM,
114 #include "util/ralloc.h"
115 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
119 glsl_base_type base_type;
121 unsigned sampler_dimensionality:3; /**< \see glsl_sampler_dim */
122 unsigned sampler_shadow:1;
123 unsigned sampler_array:1;
124 unsigned sampler_type:2; /**< Type of data returned using this
125 * sampler or image. Only \c
126 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
127 * and \c GLSL_TYPE_UINT are valid.
129 unsigned interface_packing:2;
131 /* Callers of this ralloc-based new need not call delete. It's
132 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
133 static void* operator new(size_t size)
135 mtx_lock(&glsl_type::mutex);
137 /* mem_ctx should have been created by the static members */
138 assert(glsl_type::mem_ctx != NULL);
142 type = ralloc_size(glsl_type::mem_ctx, size);
143 assert(type != NULL);
145 mtx_unlock(&glsl_type::mutex);
150 /* If the user *does* call delete, that's OK, we will just
151 * ralloc_free in that case. */
152 static void operator delete(void *type)
154 mtx_lock(&glsl_type::mutex);
156 mtx_unlock(&glsl_type::mutex);
160 * \name Vector and matrix element counts
162 * For scalars, each of these values will be 1. For non-numeric types
166 uint8_t vector_elements; /**< 1, 2, 3, or 4 vector elements. */
167 uint8_t matrix_columns; /**< 1, 2, 3, or 4 matrix columns. */
171 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
172 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
173 * elements in the structure and the number of values pointed to by
174 * \c fields.structure (below).
179 * Name of the data type
181 * Will never be \c NULL.
186 * Subtype of composite data types.
189 const struct glsl_type *array; /**< Type of array elements. */
190 const struct glsl_type *parameters; /**< Parameters to function. */
191 struct glsl_struct_field *structure; /**< List of struct fields. */
195 * \name Pointers to various public type singletons
199 #define DECL_TYPE(NAME, ...) \
200 static const glsl_type *const NAME##_type;
202 #define STRUCT_TYPE(NAME) \
203 static const glsl_type *const struct_##NAME##_type;
204 #include "builtin_type_macros.h"
208 * Convenience accessors for vector types (shorter than get_instance()).
211 static const glsl_type *vec(unsigned components);
212 static const glsl_type *dvec(unsigned components);
213 static const glsl_type *ivec(unsigned components);
214 static const glsl_type *uvec(unsigned components);
215 static const glsl_type *bvec(unsigned components);
219 * For numeric and boolean derived types returns the basic scalar type
221 * If the type is a numeric or boolean scalar, vector, or matrix type,
222 * this function gets the scalar type of the individual components. For
223 * all other types, including arrays of numeric or boolean types, the
224 * error type is returned.
226 const glsl_type *get_base_type() const;
229 * Get the basic scalar type which this type aggregates.
231 * If the type is a numeric or boolean scalar, vector, or matrix, or an
232 * array of any of those, this function gets the scalar type of the
233 * individual components. For structs and arrays of structs, this function
234 * returns the struct type. For samplers and arrays of samplers, this
235 * function returns the sampler type.
237 const glsl_type *get_scalar_type() const;
240 * Get the instance of a built-in scalar, vector, or matrix type
242 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
246 * Get the instance of a sampler type
248 static const glsl_type *get_sampler_instance(enum glsl_sampler_dim dim,
251 glsl_base_type type);
255 * Get the instance of an array type
257 static const glsl_type *get_array_instance(const glsl_type *base,
261 * Get the instance of a record type
263 static const glsl_type *get_record_instance(const glsl_struct_field *fields,
268 * Get the instance of an interface block type
270 static const glsl_type *get_interface_instance(const glsl_struct_field *fields,
272 enum glsl_interface_packing packing,
273 const char *block_name);
276 * Get the instance of an subroutine type
278 static const glsl_type *get_subroutine_instance(const char *subroutine_name);
281 * Get the type resulting from a multiplication of \p type_a * \p type_b
283 static const glsl_type *get_mul_type(const glsl_type *type_a,
284 const glsl_type *type_b);
287 * Query the total number of scalars that make up a scalar, vector or matrix
289 unsigned components() const
291 return vector_elements * matrix_columns;
295 * Calculate the number of components slots required to hold this type
297 * This is used to determine how many uniform or varying locations a type
300 unsigned component_slots() const;
303 * Calculate offset between the base location of the struct in
304 * uniform storage and a struct member.
305 * For the initial call, length is the index of the member to find the
308 unsigned record_location_offset(unsigned length) const;
311 * Calculate the number of unique values from glGetUniformLocation for the
312 * elements of the type.
314 * This is used to allocate slots in the UniformRemapTable, the amount of
315 * locations may not match with actual used storage space by the driver.
317 unsigned uniform_locations() const;
320 * Calculate the number of attribute slots required to hold this type
322 * This implements the language rules of GLSL 1.50 for counting the number
323 * of slots used by a vertex attribute. It also determines the number of
324 * varying slots the type will use up in the absence of varying packing
325 * (and thus, it can be used to measure the number of varying slots used by
326 * the varyings that are generated by lower_packed_varyings).
328 unsigned count_attribute_slots() const;
331 * Alignment in bytes of the start of this type in a std140 uniform
334 unsigned std140_base_alignment(bool row_major) const;
336 /** Size in bytes of this type in a std140 uniform block.
338 * Note that this is not GL_UNIFORM_SIZE (which is the number of
339 * elements in the array)
341 unsigned std140_size(bool row_major) const;
344 * Alignment in bytes of the start of this type in a std430 shader
347 unsigned std430_base_alignment(bool row_major) const;
350 * Calculate array stride in bytes of this type in a std430 shader storage
353 unsigned std430_array_stride(bool row_major) const;
356 * Size in bytes of this type in a std430 shader storage block.
358 * Note that this is not GL_BUFFER_SIZE
360 unsigned std430_size(bool row_major) const;
363 * \brief Can this type be implicitly converted to another?
365 * \return True if the types are identical or if this type can be converted
366 * to \c desired according to Section 4.1.10 of the GLSL spec.
369 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
370 * Implicit Conversions:
372 * In some situations, an expression and its type will be implicitly
373 * converted to a different type. The following table shows all allowed
374 * implicit conversions:
376 * Type of expression | Can be implicitly converted to
377 * --------------------------------------------------
390 * There are no implicit array or structure conversions. For example,
391 * an array of int cannot be implicitly converted to an array of float.
392 * There are no implicit conversions between signed and unsigned
396 bool can_implicitly_convert_to(const glsl_type *desired,
397 _mesa_glsl_parse_state *state) const;
400 * Query whether or not a type is a scalar (non-vector and non-matrix).
402 bool is_scalar() const
404 return (vector_elements == 1)
405 && (base_type >= GLSL_TYPE_UINT)
406 && (base_type <= GLSL_TYPE_BOOL);
410 * Query whether or not a type is a vector
412 bool is_vector() const
414 return (vector_elements > 1)
415 && (matrix_columns == 1)
416 && (base_type >= GLSL_TYPE_UINT)
417 && (base_type <= GLSL_TYPE_BOOL);
421 * Query whether or not a type is a matrix
423 bool is_matrix() const
425 /* GLSL only has float matrices. */
426 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT || base_type == GLSL_TYPE_DOUBLE);
430 * Query whether or not a type is a non-array numeric type
432 bool is_numeric() const
434 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_DOUBLE);
438 * Query whether or not a type is an integral type
440 bool is_integer() const
442 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
446 * Query whether or not type is an integral type, or for struct and array
447 * types, contains an integral type.
449 bool contains_integer() const;
452 * Query whether or not type is a double type, or for struct and array
453 * types, contains a double type.
455 bool contains_double() const;
458 * Query whether or not a type is a float type
460 bool is_float() const
462 return base_type == GLSL_TYPE_FLOAT;
466 * Query whether or not a type is a double type
468 bool is_double() const
470 return base_type == GLSL_TYPE_DOUBLE;
474 * Query whether or not a type is a non-array boolean type
476 bool is_boolean() const
478 return base_type == GLSL_TYPE_BOOL;
482 * Query whether or not a type is a sampler
484 bool is_sampler() const
486 return base_type == GLSL_TYPE_SAMPLER;
490 * Query whether or not type is a sampler, or for struct and array
491 * types, contains a sampler.
493 bool contains_sampler() const;
496 * Get the Mesa texture target index for a sampler type.
498 gl_texture_index sampler_index() const;
501 * Query whether or not type is an image, or for struct and array
502 * types, contains an image.
504 bool contains_image() const;
507 * Query whether or not a type is an image
509 bool is_image() const
511 return base_type == GLSL_TYPE_IMAGE;
515 * Query whether or not a type is an array
517 bool is_array() const
519 return base_type == GLSL_TYPE_ARRAY;
522 bool is_array_of_arrays() const
524 return is_array() && fields.array->is_array();
528 * Query whether or not a type is a record
530 bool is_record() const
532 return base_type == GLSL_TYPE_STRUCT;
536 * Query whether or not a type is an interface
538 bool is_interface() const
540 return base_type == GLSL_TYPE_INTERFACE;
544 * Query whether or not a type is the void type singleton.
548 return base_type == GLSL_TYPE_VOID;
552 * Query whether or not a type is the error type singleton.
554 bool is_error() const
556 return base_type == GLSL_TYPE_ERROR;
560 * Query if a type is unnamed/anonymous (named by the parser)
563 bool is_subroutine() const
565 return base_type == GLSL_TYPE_SUBROUTINE;
567 bool contains_subroutine() const;
569 bool is_anonymous() const
571 return !strncmp(name, "#anon", 5);
575 * Get the type stripped of any arrays
578 * Pointer to the type of elements of the first non-array type for array
579 * types, or pointer to itself for non-array types.
581 const glsl_type *without_array() const
583 const glsl_type *t = this;
585 while (t->is_array())
592 * Return the total number of elements in an array including the elements
593 * in arrays of arrays.
595 unsigned arrays_of_arrays_size() const
600 unsigned size = length;
601 const glsl_type *base_type = fields.array;
603 while (base_type->is_array()) {
604 size = size * base_type->length;
605 base_type = base_type->fields.array;
611 * Return the amount of atomic counter storage required for a type.
613 unsigned atomic_size() const
615 if (base_type == GLSL_TYPE_ATOMIC_UINT)
616 return ATOMIC_COUNTER_SIZE;
618 return length * fields.array->atomic_size();
624 * Return whether a type contains any atomic counters.
626 bool contains_atomic() const
628 return atomic_size() > 0;
632 * Return whether a type contains any opaque types.
634 bool contains_opaque() const;
637 * Query the full type of a matrix row
640 * If the type is not a matrix, \c glsl_type::error_type is returned.
641 * Otherwise a type matching the rows of the matrix is returned.
643 const glsl_type *row_type() const
646 ? get_instance(base_type, matrix_columns, 1)
651 * Query the full type of a matrix column
654 * If the type is not a matrix, \c glsl_type::error_type is returned.
655 * Otherwise a type matching the columns of the matrix is returned.
657 const glsl_type *column_type() const
660 ? get_instance(base_type, vector_elements, 1)
665 * Get the type of a structure field
668 * Pointer to the type of the named field. If the type is not a structure
669 * or the named field does not exist, \c glsl_type::error_type is returned.
671 const glsl_type *field_type(const char *name) const;
674 * Get the location of a field within a record type
676 int field_index(const char *name) const;
679 * Query the number of elements in an array type
682 * The number of elements in the array for array types or -1 for non-array
683 * types. If the number of elements in the array has not yet been declared,
686 int array_size() const
688 return is_array() ? length : -1;
692 * Query whether the array size for all dimensions has been declared.
694 bool is_unsized_array() const
696 return is_array() && length == 0;
700 * Return the number of coordinate components needed for this
701 * sampler or image type.
703 * This is based purely on the sampler's dimensionality. For example, this
704 * returns 1 for sampler1D, and 3 for sampler2DArray.
706 * Note that this is often different than actual coordinate type used in
707 * a texturing built-in function, since those pack additional values (such
708 * as the shadow comparitor or projector) into the coordinate type.
710 int coordinate_components() const;
713 * Compare a record type against another record type.
715 * This is useful for matching record types declared across shader stages.
717 bool record_compare(const glsl_type *b) const;
724 * ralloc context for all glsl_type allocations
726 * Set on the first call to \c glsl_type::new.
728 static void *mem_ctx;
730 void init_ralloc_type_ctx(void);
732 /** Constructor for vector and matrix types */
733 glsl_type(GLenum gl_type,
734 glsl_base_type base_type, unsigned vector_elements,
735 unsigned matrix_columns, const char *name);
737 /** Constructor for sampler or image types */
738 glsl_type(GLenum gl_type, glsl_base_type base_type,
739 enum glsl_sampler_dim dim, bool shadow, bool array,
740 unsigned type, const char *name);
742 /** Constructor for record types */
743 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
746 /** Constructor for interface types */
747 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
748 enum glsl_interface_packing packing, const char *name);
750 /** Constructor for array types */
751 glsl_type(const glsl_type *array, unsigned length);
753 /** Constructor for subroutine types */
754 glsl_type(const char *name);
756 /** Hash table containing the known array types. */
757 static struct hash_table *array_types;
759 /** Hash table containing the known record types. */
760 static struct hash_table *record_types;
762 /** Hash table containing the known interface types. */
763 static struct hash_table *interface_types;
765 /** Hash table containing the known subroutine types. */
766 static struct hash_table *subroutine_types;
768 static bool record_key_compare(const void *a, const void *b);
769 static unsigned record_key_hash(const void *key);
772 * \name Built-in type flyweights
776 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
778 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
779 #include "builtin_type_macros.h"
783 * \name Friend functions.
785 * These functions are friends because they must have C linkage and the
786 * need to call various private methods or access various private static
790 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
791 friend void _mesa_glsl_release_types(void);
795 struct glsl_struct_field {
796 const struct glsl_type *type;
800 * For interface blocks, gl_varying_slot corresponding to the input/output
801 * if this is a built-in input/output (i.e. a member of the built-in
802 * gl_PerVertex interface block); -1 otherwise.
804 * Ignored for structs.
809 * For interface blocks, the interpolation mode (as in
810 * ir_variable::interpolation). 0 otherwise.
812 unsigned interpolation:2;
815 * For interface blocks, 1 if this variable uses centroid interpolation (as
816 * in ir_variable::centroid). 0 otherwise.
821 * For interface blocks, 1 if this variable uses sample interpolation (as
822 * in ir_variable::sample). 0 otherwise.
827 * Layout of the matrix. Uses glsl_matrix_layout values.
829 unsigned matrix_layout:2;
832 * For interface blocks, 1 if this variable is a per-patch input or output
833 * (as in ir_variable::patch). 0 otherwise.
838 * Precision qualifier
840 unsigned precision:2;
843 * Image qualifiers, applicable to buffer variables defined in shader
844 * storage buffer objects (SSBOs)
846 unsigned image_read_only:1;
847 unsigned image_write_only:1;
848 unsigned image_coherent:1;
849 unsigned image_volatile:1;
850 unsigned image_restrict:1;
852 glsl_struct_field(const struct glsl_type *_type, const char *_name)
853 : type(_type), name(_name), location(-1), interpolation(0), centroid(0),
854 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED), patch(0),
855 precision(GLSL_PRECISION_NONE)
866 static inline unsigned int
867 glsl_align(unsigned int a, unsigned int align)
869 return (a + align - 1) / align * align;
874 #endif /* __cplusplus */
876 #endif /* GLSL_TYPES_H */