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.
31 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
37 struct _mesa_glsl_parse_state
;
38 struct glsl_symbol_table
;
41 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
);
44 _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"
109 glsl_base_type base_type
;
111 unsigned sampler_dimensionality
:3; /**< \see glsl_sampler_dim */
112 unsigned sampler_shadow
:1;
113 unsigned sampler_array
:1;
114 unsigned sampler_type
:2; /**< Type of data returned using this
115 * sampler or image. Only \c
116 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
117 * and \c GLSL_TYPE_UINT are valid.
119 unsigned interface_packing
:2;
121 /* Callers of this ralloc-based new need not call delete. It's
122 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
123 static void* operator new(size_t size
)
125 if (glsl_type::mem_ctx
== NULL
) {
126 glsl_type::mem_ctx
= ralloc_context(NULL
);
127 assert(glsl_type::mem_ctx
!= NULL
);
132 type
= ralloc_size(glsl_type::mem_ctx
, size
);
133 assert(type
!= NULL
);
138 /* If the user *does* call delete, that's OK, we will just
139 * ralloc_free in that case. */
140 static void operator delete(void *type
)
146 * \name Vector and matrix element counts
148 * For scalars, each of these values will be 1. For non-numeric types
152 unsigned vector_elements
:3; /**< 1, 2, 3, or 4 vector elements. */
153 unsigned matrix_columns
:3; /**< 1, 2, 3, or 4 matrix columns. */
157 * Name of the data type
159 * Will never be \c NULL.
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 * Subtype of composite data types.
175 const struct glsl_type
*array
; /**< Type of array elements. */
176 const struct glsl_type
*parameters
; /**< Parameters to function. */
177 struct glsl_struct_field
*structure
; /**< List of struct fields. */
181 * \name Pointers to various public type singletons
185 #define DECL_TYPE(NAME, ...) \
186 static const glsl_type *const NAME##_type;
188 #define STRUCT_TYPE(NAME) \
189 static const glsl_type *const struct_##NAME##_type;
190 #include "builtin_type_macros.h"
194 * Convenience accessors for vector types (shorter than get_instance()).
197 static const glsl_type
*vec(unsigned components
);
198 static const glsl_type
*ivec(unsigned components
);
199 static const glsl_type
*uvec(unsigned components
);
200 static const glsl_type
*bvec(unsigned components
);
204 * For numeric and boolean derived types returns the basic scalar type
206 * If the type is a numeric or boolean scalar, vector, or matrix type,
207 * this function gets the scalar type of the individual components. For
208 * all other types, including arrays of numeric or boolean types, the
209 * error type is returned.
211 const glsl_type
*get_base_type() const;
214 * Get the basic scalar type which this type aggregates.
216 * If the type is a numeric or boolean scalar, vector, or matrix, or an
217 * array of any of those, this function gets the scalar type of the
218 * individual components. For structs and arrays of structs, this function
219 * returns the struct type. For samplers and arrays of samplers, this
220 * function returns the sampler type.
222 const glsl_type
*get_scalar_type() const;
225 * Query the type of elements in an array
228 * Pointer to the type of elements in the array for array types, or \c NULL
229 * for non-array types.
231 const glsl_type
*element_type() const
233 return is_array() ? fields
.array
: NULL
;
237 * Get the instance of a built-in scalar, vector, or matrix type
239 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
243 * Get the instance of an array type
245 static const glsl_type
*get_array_instance(const glsl_type
*base
,
249 * Get the instance of a record type
251 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
256 * Get the instance of an interface block type
258 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
260 enum glsl_interface_packing packing
,
261 const char *block_name
);
264 * Query the total number of scalars that make up a scalar, vector or matrix
266 unsigned components() const
268 return vector_elements
* matrix_columns
;
272 * Calculate the number of components slots required to hold this type
274 * This is used to determine how many uniform or varying locations a type
277 unsigned component_slots() const;
280 * Calculate the number of unique values from glGetUniformLocation for the
281 * elements of the type.
283 unsigned uniform_locations() const;
286 * Calculate the number of attribute slots required to hold this type
288 * This implements the language rules of GLSL 1.50 for counting the number
289 * of slots used by a vertex attribute. It also determines the number of
290 * varying slots the type will use up in the absence of varying packing
291 * (and thus, it can be used to measure the number of varying slots used by
292 * the varyings that are generated by lower_packed_varyings).
294 unsigned count_attribute_slots() const;
298 * Alignment in bytes of the start of this type in a std140 uniform
301 unsigned std140_base_alignment(bool row_major
) const;
303 /** Size in bytes of this type in a std140 uniform block.
305 * Note that this is not GL_UNIFORM_SIZE (which is the number of
306 * elements in the array)
308 unsigned std140_size(bool row_major
) const;
311 * \brief Can this type be implicitly converted to another?
313 * \return True if the types are identical or if this type can be converted
314 * to \c desired according to Section 4.1.10 of the GLSL spec.
317 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
318 * Implicit Conversions:
320 * In some situations, an expression and its type will be implicitly
321 * converted to a different type. The following table shows all allowed
322 * implicit conversions:
324 * Type of expression | Can be implicitly converted to
325 * --------------------------------------------------
338 * There are no implicit array or structure conversions. For example,
339 * an array of int cannot be implicitly converted to an array of float.
340 * There are no implicit conversions between signed and unsigned
344 bool can_implicitly_convert_to(const glsl_type
*desired
,
345 _mesa_glsl_parse_state
*state
) const;
348 * Query whether or not a type is a scalar (non-vector and non-matrix).
350 bool is_scalar() const
352 return (vector_elements
== 1)
353 && (base_type
>= GLSL_TYPE_UINT
)
354 && (base_type
<= GLSL_TYPE_BOOL
);
358 * Query whether or not a type is a vector
360 bool is_vector() const
362 return (vector_elements
> 1)
363 && (matrix_columns
== 1)
364 && (base_type
>= GLSL_TYPE_UINT
)
365 && (base_type
<= GLSL_TYPE_BOOL
);
369 * Query whether or not a type is a matrix
371 bool is_matrix() const
373 /* GLSL only has float matrices. */
374 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
);
378 * Query whether or not a type is a non-array numeric type
380 bool is_numeric() const
382 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_FLOAT
);
386 * Query whether or not a type is an integral type
388 bool is_integer() const
390 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
394 * Query whether or not type is an integral type, or for struct and array
395 * types, contains an integral type.
397 bool contains_integer() const;
400 * Query whether or not a type is a float type
402 bool is_float() const
404 return base_type
== GLSL_TYPE_FLOAT
;
408 * Query whether or not a type is a non-array boolean type
410 bool is_boolean() const
412 return base_type
== GLSL_TYPE_BOOL
;
416 * Query whether or not a type is a sampler
418 bool is_sampler() const
420 return base_type
== GLSL_TYPE_SAMPLER
;
424 * Query whether or not type is a sampler, or for struct and array
425 * types, contains a sampler.
427 bool contains_sampler() const;
430 * Get the Mesa texture target index for a sampler type.
432 gl_texture_index
sampler_index() const;
435 * Query whether or not type is an image, or for struct and array
436 * types, contains an image.
438 bool contains_image() const;
441 * Query whether or not a type is an image
443 bool is_image() const
445 return base_type
== GLSL_TYPE_IMAGE
;
449 * Query whether or not a type is an array
451 bool is_array() const
453 return base_type
== GLSL_TYPE_ARRAY
;
457 * Query whether or not a type is a record
459 bool is_record() const
461 return base_type
== GLSL_TYPE_STRUCT
;
465 * Query whether or not a type is an interface
467 bool is_interface() const
469 return base_type
== GLSL_TYPE_INTERFACE
;
473 * Query whether or not a type is the void type singleton.
477 return base_type
== GLSL_TYPE_VOID
;
481 * Query whether or not a type is the error type singleton.
483 bool is_error() const
485 return base_type
== GLSL_TYPE_ERROR
;
489 * Get the type stripped of any arrays
492 * Pointer to the type of elements of the first non-array type for array
493 * types, or pointer to itself for non-array types.
495 const glsl_type
*without_array() const
497 return this->is_array() ? this->fields
.array
: this;
501 * Return the amount of atomic counter storage required for a type.
503 unsigned atomic_size() const
505 if (base_type
== GLSL_TYPE_ATOMIC_UINT
)
506 return ATOMIC_COUNTER_SIZE
;
508 return length
* element_type()->atomic_size();
514 * Return whether a type contains any atomic counters.
516 bool contains_atomic() const
518 return atomic_size() > 0;
522 * Return whether a type contains any opaque types.
524 bool contains_opaque() const;
527 * Query the full type of a matrix row
530 * If the type is not a matrix, \c glsl_type::error_type is returned.
531 * Otherwise a type matching the rows of the matrix is returned.
533 const glsl_type
*row_type() const
536 ? get_instance(base_type
, matrix_columns
, 1)
541 * Query the full type of a matrix column
544 * If the type is not a matrix, \c glsl_type::error_type is returned.
545 * Otherwise a type matching the columns of the matrix is returned.
547 const glsl_type
*column_type() const
550 ? get_instance(base_type
, vector_elements
, 1)
555 * Get the type of a structure field
558 * Pointer to the type of the named field. If the type is not a structure
559 * or the named field does not exist, \c glsl_type::error_type is returned.
561 const glsl_type
*field_type(const char *name
) const;
564 * Get the location of a filed within a record type
566 int field_index(const char *name
) const;
569 * Query the number of elements in an array type
572 * The number of elements in the array for array types or -1 for non-array
573 * types. If the number of elements in the array has not yet been declared,
576 int array_size() const
578 return is_array() ? length
: -1;
582 * Query whether the array size for all dimensions has been declared.
584 bool is_unsized_array() const
586 return is_array() && length
== 0;
590 * Return the number of coordinate components needed for this
591 * sampler or image type.
593 * This is based purely on the sampler's dimensionality. For example, this
594 * returns 1 for sampler1D, and 3 for sampler2DArray.
596 * Note that this is often different than actual coordinate type used in
597 * a texturing built-in function, since those pack additional values (such
598 * as the shadow comparitor or projector) into the coordinate type.
600 int coordinate_components() const;
603 * Compare a record type against another record type.
605 * This is useful for matching record types declared across shader stages.
607 bool record_compare(const glsl_type
*b
) const;
611 * ralloc context for all glsl_type allocations
613 * Set on the first call to \c glsl_type::new.
615 static void *mem_ctx
;
617 void init_ralloc_type_ctx(void);
619 /** Constructor for vector and matrix types */
620 glsl_type(GLenum gl_type
,
621 glsl_base_type base_type
, unsigned vector_elements
,
622 unsigned matrix_columns
, const char *name
);
624 /** Constructor for sampler or image types */
625 glsl_type(GLenum gl_type
, glsl_base_type base_type
,
626 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
627 unsigned type
, const char *name
);
629 /** Constructor for record types */
630 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
633 /** Constructor for interface types */
634 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
635 enum glsl_interface_packing packing
, const char *name
);
637 /** Constructor for array types */
638 glsl_type(const glsl_type
*array
, unsigned length
);
640 /** Hash table containing the known array types. */
641 static struct hash_table
*array_types
;
643 /** Hash table containing the known record types. */
644 static struct hash_table
*record_types
;
646 /** Hash table containing the known interface types. */
647 static struct hash_table
*interface_types
;
649 static int record_key_compare(const void *a
, const void *b
);
650 static unsigned record_key_hash(const void *key
);
653 * \name Built-in type flyweights
657 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
659 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
660 #include "builtin_type_macros.h"
664 * \name Friend functions.
666 * These functions are friends because they must have C linkage and the
667 * need to call various private methods or access various private static
671 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
672 friend void _mesa_glsl_release_types(void);
676 struct glsl_struct_field
{
677 const struct glsl_type
*type
;
681 * For interface blocks, gl_varying_slot corresponding to the input/output
682 * if this is a built-in input/output (i.e. a member of the built-in
683 * gl_PerVertex interface block); -1 otherwise.
685 * Ignored for structs.
690 * For interface blocks, the interpolation mode (as in
691 * ir_variable::interpolation). 0 otherwise.
693 unsigned interpolation
:2;
696 * For interface blocks, 1 if this variable uses centroid interpolation (as
697 * in ir_variable::centroid). 0 otherwise.
702 * For interface blocks, 1 if this variable uses sample interpolation (as
703 * in ir_variable::sample). 0 otherwise.
708 * Layout of the matrix. Uses glsl_matrix_layout values.
710 unsigned matrix_layout
:2;
713 * For interface blocks, it has a value if this variable uses multiple vertex
714 * streams (as in ir_variable::stream). -1 otherwise.
719 static inline unsigned int
720 glsl_align(unsigned int a
, unsigned int align
)
722 return (a
+ align
- 1) / align
* align
;
727 #endif /* __cplusplus */
729 #endif /* GLSL_TYPES_H */