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
,
65 enum glsl_sampler_dim
{
66 GLSL_SAMPLER_DIM_1D
= 0,
69 GLSL_SAMPLER_DIM_CUBE
,
70 GLSL_SAMPLER_DIM_RECT
,
72 GLSL_SAMPLER_DIM_EXTERNAL
,
76 enum glsl_interface_packing
{
77 GLSL_INTERFACE_PACKING_STD140
,
78 GLSL_INTERFACE_PACKING_SHARED
,
79 GLSL_INTERFACE_PACKING_PACKED
82 enum glsl_matrix_layout
{
84 * The layout of the matrix is inherited from the object containing the
85 * matrix (the top level structure or the uniform block).
87 GLSL_MATRIX_LAYOUT_INHERITED
,
90 * Explicit column-major layout
92 * If a uniform block doesn't have an explicit layout set, it will default
95 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
,
100 GLSL_MATRIX_LAYOUT_ROW_MAJOR
105 #include "util/ralloc.h"
106 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
110 glsl_base_type base_type
;
112 unsigned sampler_dimensionality
:3; /**< \see glsl_sampler_dim */
113 unsigned sampler_shadow
:1;
114 unsigned sampler_array
:1;
115 unsigned sampler_type
:2; /**< Type of data returned using this
116 * sampler or image. Only \c
117 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
118 * and \c GLSL_TYPE_UINT are valid.
120 unsigned interface_packing
:2;
122 /* Callers of this ralloc-based new need not call delete. It's
123 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
124 static void* operator new(size_t size
)
126 mtx_lock(&glsl_type::mutex
);
128 /* mem_ctx should have been created by the static members */
129 assert(glsl_type::mem_ctx
!= NULL
);
133 type
= ralloc_size(glsl_type::mem_ctx
, size
);
134 assert(type
!= NULL
);
136 mtx_unlock(&glsl_type::mutex
);
141 /* If the user *does* call delete, that's OK, we will just
142 * ralloc_free in that case. */
143 static void operator delete(void *type
)
145 mtx_lock(&glsl_type::mutex
);
147 mtx_unlock(&glsl_type::mutex
);
151 * \name Vector and matrix element counts
153 * For scalars, each of these values will be 1. For non-numeric types
157 uint8_t vector_elements
; /**< 1, 2, 3, or 4 vector elements. */
158 uint8_t matrix_columns
; /**< 1, 2, 3, or 4 matrix columns. */
162 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
163 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
164 * elements in the structure and the number of values pointed to by
165 * \c fields.structure (below).
170 * Name of the data type
172 * Will never be \c NULL.
177 * Subtype of composite data types.
180 const struct glsl_type
*array
; /**< Type of array elements. */
181 const struct glsl_type
*parameters
; /**< Parameters to function. */
182 struct glsl_struct_field
*structure
; /**< List of struct fields. */
186 * \name Pointers to various public type singletons
190 #define DECL_TYPE(NAME, ...) \
191 static const glsl_type *const NAME##_type;
193 #define STRUCT_TYPE(NAME) \
194 static const glsl_type *const struct_##NAME##_type;
195 #include "builtin_type_macros.h"
199 * Convenience accessors for vector types (shorter than get_instance()).
202 static const glsl_type
*vec(unsigned components
);
203 static const glsl_type
*dvec(unsigned components
);
204 static const glsl_type
*ivec(unsigned components
);
205 static const glsl_type
*uvec(unsigned components
);
206 static const glsl_type
*bvec(unsigned components
);
210 * For numeric and boolean derived types returns the basic scalar type
212 * If the type is a numeric or boolean scalar, vector, or matrix type,
213 * this function gets the scalar type of the individual components. For
214 * all other types, including arrays of numeric or boolean types, the
215 * error type is returned.
217 const glsl_type
*get_base_type() const;
220 * Get the basic scalar type which this type aggregates.
222 * If the type is a numeric or boolean scalar, vector, or matrix, or an
223 * array of any of those, this function gets the scalar type of the
224 * individual components. For structs and arrays of structs, this function
225 * returns the struct type. For samplers and arrays of samplers, this
226 * function returns the sampler type.
228 const glsl_type
*get_scalar_type() const;
231 * Get the instance of a built-in scalar, vector, or matrix type
233 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
237 * Get the instance of a sampler type
239 static const glsl_type
*get_sampler_instance(enum glsl_sampler_dim dim
,
242 glsl_base_type type
);
246 * Get the instance of an array type
248 static const glsl_type
*get_array_instance(const glsl_type
*base
,
252 * Get the instance of a record type
254 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
259 * Get the instance of an interface block type
261 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
263 enum glsl_interface_packing packing
,
264 const char *block_name
);
267 * Get the type resulting from a multiplication of \p type_a * \p type_b
269 static const glsl_type
*get_mul_type(const glsl_type
*type_a
,
270 const glsl_type
*type_b
);
273 * Query the total number of scalars that make up a scalar, vector or matrix
275 unsigned components() const
277 return vector_elements
* matrix_columns
;
281 * Calculate the number of components slots required to hold this type
283 * This is used to determine how many uniform or varying locations a type
286 unsigned component_slots() const;
289 * Calculate the number of unique values from glGetUniformLocation for the
290 * elements of the type.
292 * This is used to allocate slots in the UniformRemapTable, the amount of
293 * locations may not match with actual used storage space by the driver.
295 unsigned uniform_locations() const;
298 * Calculate the number of attribute slots required to hold this type
300 * This implements the language rules of GLSL 1.50 for counting the number
301 * of slots used by a vertex attribute. It also determines the number of
302 * varying slots the type will use up in the absence of varying packing
303 * (and thus, it can be used to measure the number of varying slots used by
304 * the varyings that are generated by lower_packed_varyings).
306 unsigned count_attribute_slots() const;
310 * Alignment in bytes of the start of this type in a std140 uniform
313 unsigned std140_base_alignment(bool row_major
) const;
315 /** Size in bytes of this type in a std140 uniform block.
317 * Note that this is not GL_UNIFORM_SIZE (which is the number of
318 * elements in the array)
320 unsigned std140_size(bool row_major
) const;
323 * \brief Can this type be implicitly converted to another?
325 * \return True if the types are identical or if this type can be converted
326 * to \c desired according to Section 4.1.10 of the GLSL spec.
329 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
330 * Implicit Conversions:
332 * In some situations, an expression and its type will be implicitly
333 * converted to a different type. The following table shows all allowed
334 * implicit conversions:
336 * Type of expression | Can be implicitly converted to
337 * --------------------------------------------------
350 * There are no implicit array or structure conversions. For example,
351 * an array of int cannot be implicitly converted to an array of float.
352 * There are no implicit conversions between signed and unsigned
356 bool can_implicitly_convert_to(const glsl_type
*desired
,
357 _mesa_glsl_parse_state
*state
) const;
360 * Query whether or not a type is a scalar (non-vector and non-matrix).
362 bool is_scalar() const
364 return (vector_elements
== 1)
365 && (base_type
>= GLSL_TYPE_UINT
)
366 && (base_type
<= GLSL_TYPE_BOOL
);
370 * Query whether or not a type is a vector
372 bool is_vector() const
374 return (vector_elements
> 1)
375 && (matrix_columns
== 1)
376 && (base_type
>= GLSL_TYPE_UINT
)
377 && (base_type
<= GLSL_TYPE_BOOL
);
381 * Query whether or not a type is a matrix
383 bool is_matrix() const
385 /* GLSL only has float matrices. */
386 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
|| base_type
== GLSL_TYPE_DOUBLE
);
390 * Query whether or not a type is a non-array numeric type
392 bool is_numeric() const
394 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_DOUBLE
);
398 * Query whether or not a type is an integral type
400 bool is_integer() const
402 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
406 * Query whether or not type is an integral type, or for struct and array
407 * types, contains an integral type.
409 bool contains_integer() const;
412 * Query whether or not type is a double type, or for struct and array
413 * types, contains a double type.
415 bool contains_double() const;
418 * Query whether or not a type is a float type
420 bool is_float() const
422 return base_type
== GLSL_TYPE_FLOAT
;
426 * Query whether or not a type is a double type
428 bool is_double() const
430 return base_type
== GLSL_TYPE_DOUBLE
;
434 * Query whether or not a type is a non-array boolean type
436 bool is_boolean() const
438 return base_type
== GLSL_TYPE_BOOL
;
442 * Query whether or not a type is a sampler
444 bool is_sampler() const
446 return base_type
== GLSL_TYPE_SAMPLER
;
450 * Query whether or not type is a sampler, or for struct and array
451 * types, contains a sampler.
453 bool contains_sampler() const;
456 * Get the Mesa texture target index for a sampler type.
458 gl_texture_index
sampler_index() const;
461 * Query whether or not type is an image, or for struct and array
462 * types, contains an image.
464 bool contains_image() const;
467 * Query whether or not a type is an image
469 bool is_image() const
471 return base_type
== GLSL_TYPE_IMAGE
;
475 * Query whether or not a type is an array
477 bool is_array() const
479 return base_type
== GLSL_TYPE_ARRAY
;
483 * Query whether or not a type is a record
485 bool is_record() const
487 return base_type
== GLSL_TYPE_STRUCT
;
491 * Query whether or not a type is an interface
493 bool is_interface() const
495 return base_type
== GLSL_TYPE_INTERFACE
;
499 * Query whether or not a type is the void type singleton.
503 return base_type
== GLSL_TYPE_VOID
;
507 * Query whether or not a type is the error type singleton.
509 bool is_error() const
511 return base_type
== GLSL_TYPE_ERROR
;
515 * Query if a type is unnamed/anonymous (named by the parser)
517 bool is_anonymous() const
519 return !strncmp(name
, "#anon", 5);
523 * Get the type stripped of any arrays
526 * Pointer to the type of elements of the first non-array type for array
527 * types, or pointer to itself for non-array types.
529 const glsl_type
*without_array() const
531 const glsl_type
*t
= this;
533 while (t
->is_array())
540 * Return the amount of atomic counter storage required for a type.
542 unsigned atomic_size() const
544 if (base_type
== GLSL_TYPE_ATOMIC_UINT
)
545 return ATOMIC_COUNTER_SIZE
;
547 return length
* fields
.array
->atomic_size();
553 * Return whether a type contains any atomic counters.
555 bool contains_atomic() const
557 return atomic_size() > 0;
561 * Return whether a type contains any opaque types.
563 bool contains_opaque() const;
566 * Query the full type of a matrix row
569 * If the type is not a matrix, \c glsl_type::error_type is returned.
570 * Otherwise a type matching the rows of the matrix is returned.
572 const glsl_type
*row_type() const
575 ? get_instance(base_type
, matrix_columns
, 1)
580 * Query the full type of a matrix column
583 * If the type is not a matrix, \c glsl_type::error_type is returned.
584 * Otherwise a type matching the columns of the matrix is returned.
586 const glsl_type
*column_type() const
589 ? get_instance(base_type
, vector_elements
, 1)
594 * Get the type of a structure field
597 * Pointer to the type of the named field. If the type is not a structure
598 * or the named field does not exist, \c glsl_type::error_type is returned.
600 const glsl_type
*field_type(const char *name
) const;
603 * Get the location of a filed within a record type
605 int field_index(const char *name
) const;
608 * Query the number of elements in an array type
611 * The number of elements in the array for array types or -1 for non-array
612 * types. If the number of elements in the array has not yet been declared,
615 int array_size() const
617 return is_array() ? length
: -1;
621 * Query whether the array size for all dimensions has been declared.
623 bool is_unsized_array() const
625 return is_array() && length
== 0;
629 * Return the number of coordinate components needed for this
630 * sampler or image type.
632 * This is based purely on the sampler's dimensionality. For example, this
633 * returns 1 for sampler1D, and 3 for sampler2DArray.
635 * Note that this is often different than actual coordinate type used in
636 * a texturing built-in function, since those pack additional values (such
637 * as the shadow comparitor or projector) into the coordinate type.
639 int coordinate_components() const;
642 * Compare a record type against another record type.
644 * This is useful for matching record types declared across shader stages.
646 bool record_compare(const glsl_type
*b
) const;
653 * ralloc context for all glsl_type allocations
655 * Set on the first call to \c glsl_type::new.
657 static void *mem_ctx
;
659 void init_ralloc_type_ctx(void);
661 /** Constructor for vector and matrix types */
662 glsl_type(GLenum gl_type
,
663 glsl_base_type base_type
, unsigned vector_elements
,
664 unsigned matrix_columns
, const char *name
);
666 /** Constructor for sampler or image types */
667 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
668 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
669 unsigned type
, const char *name
);
671 /** Constructor for record types */
672 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
675 /** Constructor for interface types */
676 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
677 enum glsl_interface_packing packing
, const char *name
);
679 /** Constructor for array types */
680 glsl_type(const glsl_type
*array
, unsigned length
);
682 /** Hash table containing the known array types. */
683 static struct hash_table
*array_types
;
685 /** Hash table containing the known record types. */
686 static struct hash_table
*record_types
;
688 /** Hash table containing the known interface types. */
689 static struct hash_table
*interface_types
;
691 static bool record_key_compare(const void *a
, const void *b
);
692 static unsigned record_key_hash(const void *key
);
695 * \name Built-in type flyweights
699 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
701 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
702 #include "builtin_type_macros.h"
706 * \name Friend functions.
708 * These functions are friends because they must have C linkage and the
709 * need to call various private methods or access various private static
713 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
714 friend void _mesa_glsl_release_types(void);
718 struct glsl_struct_field
{
719 const struct glsl_type
*type
;
723 * For interface blocks, gl_varying_slot corresponding to the input/output
724 * if this is a built-in input/output (i.e. a member of the built-in
725 * gl_PerVertex interface block); -1 otherwise.
727 * Ignored for structs.
732 * For interface blocks, the interpolation mode (as in
733 * ir_variable::interpolation). 0 otherwise.
735 unsigned interpolation
:2;
738 * For interface blocks, 1 if this variable uses centroid interpolation (as
739 * in ir_variable::centroid). 0 otherwise.
744 * For interface blocks, 1 if this variable uses sample interpolation (as
745 * in ir_variable::sample). 0 otherwise.
750 * Layout of the matrix. Uses glsl_matrix_layout values.
752 unsigned matrix_layout
:2;
755 * For interface blocks, 1 if this variable is a per-patch input or output
756 * (as in ir_variable::patch). 0 otherwise.
761 * For interface blocks, it has a value if this variable uses multiple vertex
762 * streams (as in ir_variable::stream). -1 otherwise.
767 static inline unsigned int
768 glsl_align(unsigned int a
, unsigned int align
)
770 return (a
+ align
- 1) / align
* align
;
775 #endif /* __cplusplus */
777 #endif /* GLSL_TYPES_H */