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
73 enum glsl_interface_packing
{
74 GLSL_INTERFACE_PACKING_STD140
,
75 GLSL_INTERFACE_PACKING_SHARED
,
76 GLSL_INTERFACE_PACKING_PACKED
85 glsl_base_type base_type
;
87 unsigned sampler_dimensionality
:3; /**< \see glsl_sampler_dim */
88 unsigned sampler_shadow
:1;
89 unsigned sampler_array
:1;
90 unsigned sampler_type
:2; /**< Type of data returned using this sampler.
91 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
92 * and \c GLSL_TYPE_UINT are valid.
94 unsigned interface_packing
:2;
96 /* Callers of this ralloc-based new need not call delete. It's
97 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
98 static void* operator new(size_t size
)
100 if (glsl_type::mem_ctx
== NULL
) {
101 glsl_type::mem_ctx
= ralloc_context(NULL
);
102 assert(glsl_type::mem_ctx
!= NULL
);
107 type
= ralloc_size(glsl_type::mem_ctx
, size
);
108 assert(type
!= NULL
);
113 /* If the user *does* call delete, that's OK, we will just
114 * ralloc_free in that case. */
115 static void operator delete(void *type
)
121 * \name Vector and matrix element counts
123 * For scalars, each of these values will be 1. For non-numeric types
127 unsigned vector_elements
:3; /**< 1, 2, 3, or 4 vector elements. */
128 unsigned matrix_columns
:3; /**< 1, 2, 3, or 4 matrix columns. */
132 * Name of the data type
134 * This may be \c NULL for anonymous structures, for arrays, or for
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. */
158 * \name Pointers to various public type singletons
161 static const glsl_type
*const error_type
;
162 static const glsl_type
*const void_type
;
163 static const glsl_type
*const int_type
;
164 static const glsl_type
*const ivec2_type
;
165 static const glsl_type
*const ivec3_type
;
166 static const glsl_type
*const ivec4_type
;
167 static const glsl_type
*const uint_type
;
168 static const glsl_type
*const uvec2_type
;
169 static const glsl_type
*const uvec3_type
;
170 static const glsl_type
*const uvec4_type
;
171 static const glsl_type
*const float_type
;
172 static const glsl_type
*const vec2_type
;
173 static const glsl_type
*const vec3_type
;
174 static const glsl_type
*const vec4_type
;
175 static const glsl_type
*const bool_type
;
176 static const glsl_type
*const bvec2_type
;
177 static const glsl_type
*const bvec3_type
;
178 static const glsl_type
*const bvec4_type
;
179 static const glsl_type
*const mat2_type
;
180 static const glsl_type
*const mat2x3_type
;
181 static const glsl_type
*const mat2x4_type
;
182 static const glsl_type
*const mat3x2_type
;
183 static const glsl_type
*const mat3_type
;
184 static const glsl_type
*const mat3x4_type
;
185 static const glsl_type
*const mat4x2_type
;
186 static const glsl_type
*const mat4x3_type
;
187 static const glsl_type
*const mat4_type
;
192 * For numeric and boolean derrived types returns the basic scalar type
194 * If the type is a numeric or boolean scalar, vector, or matrix type,
195 * this function gets the scalar type of the individual components. For
196 * all other types, including arrays of numeric or boolean types, the
197 * error type is returned.
199 const glsl_type
*get_base_type() const;
202 * Get the basic scalar type which this type aggregates.
204 * If the type is a numeric or boolean scalar, vector, or matrix, or an
205 * array of any of those, this function gets the scalar type of the
206 * individual components. For structs and arrays of structs, this function
207 * returns the struct type. For samplers and arrays of samplers, this
208 * function returns the sampler type.
210 const glsl_type
*get_scalar_type() const;
213 * Query the type of elements in an array
216 * Pointer to the type of elements in the array for array types, or \c NULL
217 * for non-array types.
219 const glsl_type
*element_type() const
221 return is_array() ? fields
.array
: NULL
;
225 * Get the instance of a built-in scalar, vector, or matrix type
227 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
231 * Get the instance of an array type
233 static const glsl_type
*get_array_instance(const glsl_type
*base
,
237 * Get the instance of a record type
239 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
244 * Get the instance of an interface block type
246 static const glsl_type
*get_interface_instance(const glsl_struct_field
*fields
,
248 enum glsl_interface_packing packing
,
252 * Query the total number of scalars that make up a scalar, vector or matrix
254 unsigned components() const
256 return vector_elements
* matrix_columns
;
260 * Calculate the number of components slots required to hold this type
262 * This is used to determine how many uniform or varying locations a type
265 unsigned component_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 a type is a float type
365 bool is_float() const
367 return base_type
== GLSL_TYPE_FLOAT
;
371 * Query whether or not a type is a non-array boolean type
373 bool is_boolean() const
375 return base_type
== GLSL_TYPE_BOOL
;
379 * Query whether or not a type is a sampler
381 bool is_sampler() const
383 return base_type
== GLSL_TYPE_SAMPLER
;
387 * Query whether or not type is a sampler, or for struct and array
388 * types, contains a sampler.
390 bool contains_sampler() const;
393 * Get the Mesa texture target index for a sampler type.
395 gl_texture_index
sampler_index() const;
398 * Query whether or not a type is an array
400 bool is_array() const
402 return base_type
== GLSL_TYPE_ARRAY
;
406 * Query whether or not a type is a record
408 bool is_record() const
410 return base_type
== GLSL_TYPE_STRUCT
;
414 * Query whether or not a type is an interface
416 bool is_interface() const
418 return base_type
== GLSL_TYPE_INTERFACE
;
422 * Query whether or not a type is the void type singleton.
426 return base_type
== GLSL_TYPE_VOID
;
430 * Query whether or not a type is the error type singleton.
432 bool is_error() const
434 return base_type
== GLSL_TYPE_ERROR
;
438 * Query the full type of a matrix row
441 * If the type is not a matrix, \c glsl_type::error_type is returned.
442 * Otherwise a type matching the rows of the matrix is returned.
444 const glsl_type
*row_type() const
447 ? get_instance(base_type
, matrix_columns
, 1)
452 * Query the full type of a matrix column
455 * If the type is not a matrix, \c glsl_type::error_type is returned.
456 * Otherwise a type matching the columns of the matrix is returned.
458 const glsl_type
*column_type() const
461 ? get_instance(base_type
, vector_elements
, 1)
467 * Get the type of a structure field
470 * Pointer to the type of the named field. If the type is not a structure
471 * or the named field does not exist, \c glsl_type::error_type is returned.
473 const glsl_type
*field_type(const char *name
) const;
477 * Get the location of a filed within a record type
479 int field_index(const char *name
) const;
483 * Query the number of elements in an array type
486 * The number of elements in the array for array types or -1 for non-array
487 * types. If the number of elements in the array has not yet been declared,
490 int array_size() const
492 return is_array() ? length
: -1;
497 * ralloc context for all glsl_type allocations
499 * Set on the first call to \c glsl_type::new.
501 static void *mem_ctx
;
503 void init_ralloc_type_ctx(void);
505 /** Constructor for vector and matrix types */
506 glsl_type(GLenum gl_type
,
507 glsl_base_type base_type
, unsigned vector_elements
,
508 unsigned matrix_columns
, const char *name
);
510 /** Constructor for sampler types */
511 glsl_type(GLenum gl_type
,
512 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
513 unsigned type
, const char *name
);
515 /** Constructor for record types */
516 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
519 /** Constructor for interface types */
520 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
521 enum glsl_interface_packing packing
, const char *name
);
523 /** Constructor for array types */
524 glsl_type(const glsl_type
*array
, unsigned length
);
526 /** Hash table containing the known array types. */
527 static struct hash_table
*array_types
;
529 /** Hash table containing the known record types. */
530 static struct hash_table
*record_types
;
532 /** Hash table containing the known interface types. */
533 static struct hash_table
*interface_types
;
535 static int record_key_compare(const void *a
, const void *b
);
536 static unsigned record_key_hash(const void *key
);
539 * \name Pointers to various type singletons
542 static const glsl_type _error_type
;
543 static const glsl_type _void_type
;
544 static const glsl_type _sampler3D_type
;
545 static const glsl_type _samplerCubeShadow_type
;
546 static const glsl_type builtin_core_types
[];
547 static const glsl_type builtin_structure_types
[];
548 static const glsl_type builtin_110_deprecated_structure_types
[];
549 static const glsl_type builtin_110_types
[];
550 static const glsl_type builtin_120_types
[];
551 static const glsl_type builtin_130_types
[];
552 static const glsl_type builtin_140_types
[];
553 static const glsl_type builtin_ARB_texture_rectangle_types
[];
554 static const glsl_type builtin_EXT_texture_array_types
[];
555 static const glsl_type builtin_EXT_texture_buffer_object_types
[];
556 static const glsl_type builtin_OES_EGL_image_external_types
[];
557 static const glsl_type builtin_ARB_texture_cube_map_array_types
[];
561 * \name Methods to populate a symbol table with built-in types.
564 * This is one of the truely annoying things about C++. Methods that are
565 * completely internal and private to a type still have to be advertised to
566 * the world in a public header file.
569 static void generate_100ES_types(glsl_symbol_table
*);
570 static void generate_300ES_types(glsl_symbol_table
*);
571 static void generate_110_types(glsl_symbol_table
*, bool add_deprecated
,
573 static void generate_120_types(glsl_symbol_table
*, bool add_deprecated
,
575 static void generate_130_types(glsl_symbol_table
*, bool add_deprecated
,
577 static void generate_140_types(glsl_symbol_table
*);
578 static void generate_ARB_texture_rectangle_types(glsl_symbol_table
*, bool);
579 static void generate_EXT_texture_array_types(glsl_symbol_table
*, bool);
580 static void generate_OES_texture_3D_types(glsl_symbol_table
*, bool);
581 static void generate_OES_EGL_image_external_types(glsl_symbol_table
*, bool);
582 static void generate_ARB_texture_cube_map_array_types(glsl_symbol_table
*, bool);
586 * \name Friend functions.
588 * These functions are friends because they must have C linkage and the
589 * need to call various private methods or access various private static
593 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
594 friend void _mesa_glsl_release_types(void);
598 struct glsl_struct_field
{
599 const struct glsl_type
*type
;
604 static inline unsigned int
605 glsl_align(unsigned int a
, unsigned int align
)
607 return (a
+ align
- 1) / align
* align
;
610 #endif /* __cplusplus */
612 #endif /* GLSL_TYPES_H */