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);
63 enum glsl_sampler_dim
{
64 GLSL_SAMPLER_DIM_1D
= 0,
67 GLSL_SAMPLER_DIM_CUBE
,
68 GLSL_SAMPLER_DIM_RECT
,
70 GLSL_SAMPLER_DIM_EXTERNAL
,
74 enum glsl_interface_packing
{
75 GLSL_INTERFACE_PACKING_STD140
,
76 GLSL_INTERFACE_PACKING_SHARED
,
77 GLSL_INTERFACE_PACKING_PACKED
86 glsl_base_type base_type
;
88 unsigned sampler_dimensionality
:3; /**< \see glsl_sampler_dim */
89 unsigned sampler_shadow
:1;
90 unsigned sampler_array
:1;
91 unsigned sampler_type
:2; /**< Type of data returned using this sampler.
92 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
93 * and \c GLSL_TYPE_UINT are valid.
95 unsigned interface_packing
:2;
97 /* Callers of this ralloc-based new need not call delete. It's
98 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
99 static void* operator new(size_t size
)
101 if (glsl_type::mem_ctx
== NULL
) {
102 glsl_type::mem_ctx
= ralloc_context(NULL
);
103 assert(glsl_type::mem_ctx
!= NULL
);
108 type
= ralloc_size(glsl_type::mem_ctx
, size
);
109 assert(type
!= NULL
);
114 /* If the user *does* call delete, that's OK, we will just
115 * ralloc_free in that case. */
116 static void operator delete(void *type
)
122 * \name Vector and matrix element counts
124 * For scalars, each of these values will be 1. For non-numeric types
128 unsigned vector_elements
:3; /**< 1, 2, 3, or 4 vector elements. */
129 unsigned matrix_columns
:3; /**< 1, 2, 3, or 4 matrix columns. */
133 * Name of the data type
135 * Will never be \c NULL.
140 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
141 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
142 * elements in the structure and the number of values pointed to by
143 * \c fields.structure (below).
148 * Subtype of composite data types.
151 const struct glsl_type
*array
; /**< Type of array elements. */
152 const struct glsl_type
*parameters
; /**< Parameters to function. */
153 struct glsl_struct_field
*structure
; /**< List of struct fields. */
157 * \name Pointers to various public type singletons
161 #define DECL_TYPE(NAME, ...) \
162 static const glsl_type *const NAME##_type;
164 #define STRUCT_TYPE(NAME) \
165 static const glsl_type *const struct_##NAME##_type;
166 #include "builtin_type_macros.h"
170 * Convenience accessors for vector types (shorter than get_instance()).
173 static const glsl_type
*vec(unsigned components
);
174 static const glsl_type
*ivec(unsigned components
);
175 static const glsl_type
*uvec(unsigned components
);
176 static const glsl_type
*bvec(unsigned components
);
180 * For numeric and boolean derrived types returns the basic scalar type
182 * If the type is a numeric or boolean scalar, vector, or matrix type,
183 * this function gets the scalar type of the individual components. For
184 * all other types, including arrays of numeric or boolean types, the
185 * error type is returned.
187 const glsl_type
*get_base_type() const;
190 * Get the basic scalar type which this type aggregates.
192 * If the type is a numeric or boolean scalar, vector, or matrix, or an
193 * array of any of those, this function gets the scalar type of the
194 * individual components. For structs and arrays of structs, this function
195 * returns the struct type. For samplers and arrays of samplers, this
196 * function returns the sampler type.
198 const glsl_type
*get_scalar_type() const;
201 * Query the type of elements in an array
204 * Pointer to the type of elements in the array for array types, or \c NULL
205 * for non-array types.
207 const glsl_type
*element_type() const
209 return is_array() ? fields
.array
: NULL
;
213 * Get the instance of a built-in scalar, vector, or matrix type
215 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
219 * Get the instance of an array type
221 static const glsl_type
*get_array_instance(const glsl_type
*base
,
225 * Get the instance of a record type
227 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
232 * Get the instance of an interface block type
234 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
236 enum glsl_interface_packing packing
,
237 const char *block_name
);
240 * Query the total number of scalars that make up a scalar, vector or matrix
242 unsigned components() const
244 return vector_elements
* matrix_columns
;
248 * Calculate the number of components slots required to hold this type
250 * This is used to determine how many uniform or varying locations a type
253 unsigned component_slots() const;
256 * Calculate the number of attribute slots required to hold this type
258 * This implements the language rules of GLSL 1.50 for counting the number
259 * of slots used by a vertex attribute. It also determines the number of
260 * varying slots the type will use up in the absence of varying packing
261 * (and thus, it can be used to measure the number of varying slots used by
262 * the varyings that are generated by lower_packed_varyings).
264 unsigned count_attribute_slots() const;
268 * Alignment in bytes of the start of this type in a std140 uniform
271 unsigned std140_base_alignment(bool row_major
) const;
273 /** Size in bytes of this type in a std140 uniform block.
275 * Note that this is not GL_UNIFORM_SIZE (which is the number of
276 * elements in the array)
278 unsigned std140_size(bool row_major
) const;
281 * \brief Can this type be implicitly converted to another?
283 * \return True if the types are identical or if this type can be converted
284 * to \c desired according to Section 4.1.10 of the GLSL spec.
287 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
288 * Implicit Conversions:
290 * In some situations, an expression and its type will be implicitly
291 * converted to a different type. The following table shows all allowed
292 * implicit conversions:
294 * Type of expression | Can be implicitly converted to
295 * --------------------------------------------------
308 * There are no implicit array or structure conversions. For example,
309 * an array of int cannot be implicitly converted to an array of float.
310 * There are no implicit conversions between signed and unsigned
314 bool can_implicitly_convert_to(const glsl_type
*desired
) const;
317 * Query whether or not a type is a scalar (non-vector and non-matrix).
319 bool is_scalar() const
321 return (vector_elements
== 1)
322 && (base_type
>= GLSL_TYPE_UINT
)
323 && (base_type
<= GLSL_TYPE_BOOL
);
327 * Query whether or not a type is a vector
329 bool is_vector() const
331 return (vector_elements
> 1)
332 && (matrix_columns
== 1)
333 && (base_type
>= GLSL_TYPE_UINT
)
334 && (base_type
<= GLSL_TYPE_BOOL
);
338 * Query whether or not a type is a matrix
340 bool is_matrix() const
342 /* GLSL only has float matrices. */
343 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
);
347 * Query whether or not a type is a non-array numeric type
349 bool is_numeric() const
351 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_FLOAT
);
355 * Query whether or not a type is an integral type
357 bool is_integer() const
359 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
363 * Query whether or not type is an integral type, or for struct and array
364 * types, contains an integral type.
366 bool contains_integer() const;
369 * Query whether or not a type is a float type
371 bool is_float() const
373 return base_type
== GLSL_TYPE_FLOAT
;
377 * Query whether or not a type is a non-array boolean type
379 bool is_boolean() const
381 return base_type
== GLSL_TYPE_BOOL
;
385 * Query whether or not a type is a sampler
387 bool is_sampler() const
389 return base_type
== GLSL_TYPE_SAMPLER
;
393 * Query whether or not type is a sampler, or for struct and array
394 * types, contains a sampler.
396 bool contains_sampler() const;
399 * Get the Mesa texture target index for a sampler type.
401 gl_texture_index
sampler_index() const;
404 * Query whether or not a type is an array
406 bool is_array() const
408 return base_type
== GLSL_TYPE_ARRAY
;
412 * Query whether or not a type is a record
414 bool is_record() const
416 return base_type
== GLSL_TYPE_STRUCT
;
420 * Query whether or not a type is an interface
422 bool is_interface() const
424 return base_type
== GLSL_TYPE_INTERFACE
;
428 * Query whether or not a type is the void type singleton.
432 return base_type
== GLSL_TYPE_VOID
;
436 * Query whether or not a type is the error type singleton.
438 bool is_error() const
440 return base_type
== GLSL_TYPE_ERROR
;
444 * Query the full type of a matrix row
447 * If the type is not a matrix, \c glsl_type::error_type is returned.
448 * Otherwise a type matching the rows of the matrix is returned.
450 const glsl_type
*row_type() const
453 ? get_instance(base_type
, matrix_columns
, 1)
458 * Query the full type of a matrix column
461 * If the type is not a matrix, \c glsl_type::error_type is returned.
462 * Otherwise a type matching the columns of the matrix is returned.
464 const glsl_type
*column_type() const
467 ? get_instance(base_type
, vector_elements
, 1)
472 * Get the type of a structure field
475 * Pointer to the type of the named field. If the type is not a structure
476 * or the named field does not exist, \c glsl_type::error_type is returned.
478 const glsl_type
*field_type(const char *name
) const;
481 * Get the location of a filed within a record type
483 int field_index(const char *name
) const;
486 * Query the number of elements in an array type
489 * The number of elements in the array for array types or -1 for non-array
490 * types. If the number of elements in the array has not yet been declared,
493 int array_size() const
495 return is_array() ? length
: -1;
499 * Query whether the array size for all dimensions has been declared.
501 bool is_unsized_array() const
503 return is_array() && length
== 0;
507 * Return the number of coordinate components needed for this sampler type.
509 * This is based purely on the sampler's dimensionality. For example, this
510 * returns 1 for sampler1D, and 3 for sampler2DArray.
512 * Note that this is often different than actual coordinate type used in
513 * a texturing built-in function, since those pack additional values (such
514 * as the shadow comparitor or projector) into the coordinate type.
516 int sampler_coordinate_components() const;
520 * ralloc context for all glsl_type allocations
522 * Set on the first call to \c glsl_type::new.
524 static void *mem_ctx
;
526 void init_ralloc_type_ctx(void);
528 /** Constructor for vector and matrix types */
529 glsl_type(GLenum gl_type
,
530 glsl_base_type base_type
, unsigned vector_elements
,
531 unsigned matrix_columns
, const char *name
);
533 /** Constructor for sampler types */
534 glsl_type(GLenum gl_type
,
535 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
536 unsigned type
, const char *name
);
538 /** Constructor for record types */
539 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
542 /** Constructor for interface types */
543 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
544 enum glsl_interface_packing packing
, const char *name
);
546 /** Constructor for array types */
547 glsl_type(const glsl_type
*array
, unsigned length
);
549 /** Hash table containing the known array types. */
550 static struct hash_table
*array_types
;
552 /** Hash table containing the known record types. */
553 static struct hash_table
*record_types
;
555 /** Hash table containing the known interface types. */
556 static struct hash_table
*interface_types
;
558 static int record_key_compare(const void *a
, const void *b
);
559 static unsigned record_key_hash(const void *key
);
562 * \name Built-in type flyweights
566 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
568 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
569 #include "builtin_type_macros.h"
573 * \name Friend functions.
575 * These functions are friends because they must have C linkage and the
576 * need to call various private methods or access various private static
580 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
581 friend void _mesa_glsl_release_types(void);
585 struct glsl_struct_field
{
586 const struct glsl_type
*type
;
591 * For interface blocks, gl_varying_slot corresponding to the input/output
592 * if this is a built-in input/output (i.e. a member of the built-in
593 * gl_PerVertex interface block); -1 otherwise.
595 * Ignored for structs.
600 * For interface blocks, the interpolation mode (as in
601 * ir_variable::interpolation). 0 otherwise.
603 unsigned interpolation
:2;
606 * For interface blocks, 1 if this variable uses centroid interpolation (as
607 * in ir_variable::centroid). 0 otherwise.
612 static inline unsigned int
613 glsl_align(unsigned int a
, unsigned int align
)
615 return (a
+ align
- 1) / align
* align
;
620 #endif /* __cplusplus */
622 #endif /* GLSL_TYPES_H */