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);
62 enum glsl_sampler_dim
{
63 GLSL_SAMPLER_DIM_1D
= 0,
66 GLSL_SAMPLER_DIM_CUBE
,
67 GLSL_SAMPLER_DIM_RECT
,
69 GLSL_SAMPLER_DIM_EXTERNAL
78 glsl_base_type base_type
;
80 unsigned sampler_dimensionality
:3; /**< \see glsl_sampler_dim */
81 unsigned sampler_shadow
:1;
82 unsigned sampler_array
:1;
83 unsigned sampler_type
:2; /**< Type of data returned using this sampler.
84 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
85 * and \c GLSL_TYPE_UINT are valid.
88 /* Callers of this ralloc-based new need not call delete. It's
89 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
90 static void* operator new(size_t size
)
92 if (glsl_type::mem_ctx
== NULL
) {
93 glsl_type::mem_ctx
= ralloc_context(NULL
);
94 assert(glsl_type::mem_ctx
!= NULL
);
99 type
= ralloc_size(glsl_type::mem_ctx
, size
);
100 assert(type
!= NULL
);
105 /* If the user *does* call delete, that's OK, we will just
106 * ralloc_free in that case. */
107 static void operator delete(void *type
)
113 * \name Vector and matrix element counts
115 * For scalars, each of these values will be 1. For non-numeric types
119 unsigned vector_elements
:3; /**< 1, 2, 3, or 4 vector elements. */
120 unsigned matrix_columns
:3; /**< 1, 2, 3, or 4 matrix columns. */
124 * Name of the data type
126 * This may be \c NULL for anonymous structures, for arrays, or for
132 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
133 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
134 * the number of values pointed to by \c fields.structure (below).
139 * Subtype of composite data types.
142 const struct glsl_type
*array
; /**< Type of array elements. */
143 const struct glsl_type
*parameters
; /**< Parameters to function. */
144 struct glsl_struct_field
*structure
; /**< List of struct fields. */
149 * \name Pointers to various public type singletons
152 static const glsl_type
*const error_type
;
153 static const glsl_type
*const void_type
;
154 static const glsl_type
*const int_type
;
155 static const glsl_type
*const ivec4_type
;
156 static const glsl_type
*const uint_type
;
157 static const glsl_type
*const uvec2_type
;
158 static const glsl_type
*const uvec3_type
;
159 static const glsl_type
*const uvec4_type
;
160 static const glsl_type
*const float_type
;
161 static const glsl_type
*const vec2_type
;
162 static const glsl_type
*const vec3_type
;
163 static const glsl_type
*const vec4_type
;
164 static const glsl_type
*const bool_type
;
165 static const glsl_type
*const mat2_type
;
166 static const glsl_type
*const mat2x3_type
;
167 static const glsl_type
*const mat2x4_type
;
168 static const glsl_type
*const mat3x2_type
;
169 static const glsl_type
*const mat3_type
;
170 static const glsl_type
*const mat3x4_type
;
171 static const glsl_type
*const mat4x2_type
;
172 static const glsl_type
*const mat4x3_type
;
173 static const glsl_type
*const mat4_type
;
178 * For numeric and boolean derrived types returns the basic scalar type
180 * If the type is a numeric or boolean scalar, vector, or matrix type,
181 * this function gets the scalar type of the individual components. For
182 * all other types, including arrays of numeric or boolean types, the
183 * error type is returned.
185 const glsl_type
*get_base_type() const;
188 * Get the basic scalar type which this type aggregates.
190 * If the type is a numeric or boolean scalar, vector, or matrix, or an
191 * array of any of those, this function gets the scalar type of the
192 * individual components. For structs and arrays of structs, this function
193 * returns the struct type. For samplers and arrays of samplers, this
194 * function returns the sampler type.
196 const glsl_type
*get_scalar_type() const;
199 * Query the type of elements in an array
202 * Pointer to the type of elements in the array for array types, or \c NULL
203 * for non-array types.
205 const glsl_type
*element_type() const
207 return is_array() ? fields
.array
: NULL
;
211 * Get the instance of a built-in scalar, vector, or matrix type
213 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
217 * Get the instance of an array type
219 static const glsl_type
*get_array_instance(const glsl_type
*base
,
223 * Get the instance of a record type
225 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
230 * Query the total number of scalars that make up a scalar, vector or matrix
232 unsigned components() const
234 return vector_elements
* matrix_columns
;
238 * Calculate the number of components slots required to hold this type
240 * This is used to determine how many uniform or varying locations a type
243 unsigned component_slots() const;
246 * \brief Can this type be implicitly converted to another?
248 * \return True if the types are identical or if this type can be converted
249 * to \c desired according to Section 4.1.10 of the GLSL spec.
252 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
253 * Implicit Conversions:
255 * In some situations, an expression and its type will be implicitly
256 * converted to a different type. The following table shows all allowed
257 * implicit conversions:
259 * Type of expression | Can be implicitly converted to
260 * --------------------------------------------------
273 * There are no implicit array or structure conversions. For example,
274 * an array of int cannot be implicitly converted to an array of float.
275 * There are no implicit conversions between signed and unsigned
279 bool can_implicitly_convert_to(const glsl_type
*desired
) const;
282 * Query whether or not a type is a scalar (non-vector and non-matrix).
284 bool is_scalar() const
286 return (vector_elements
== 1)
287 && (base_type
>= GLSL_TYPE_UINT
)
288 && (base_type
<= GLSL_TYPE_BOOL
);
292 * Query whether or not a type is a vector
294 bool is_vector() const
296 return (vector_elements
> 1)
297 && (matrix_columns
== 1)
298 && (base_type
>= GLSL_TYPE_UINT
)
299 && (base_type
<= GLSL_TYPE_BOOL
);
303 * Query whether or not a type is a matrix
305 bool is_matrix() const
307 /* GLSL only has float matrices. */
308 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
);
312 * Query whether or not a type is a non-array numeric type
314 bool is_numeric() const
316 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_FLOAT
);
320 * Query whether or not a type is an integral type
322 bool is_integer() const
324 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
328 * Query whether or not a type is a float type
330 bool is_float() const
332 return base_type
== GLSL_TYPE_FLOAT
;
336 * Query whether or not a type is a non-array boolean type
338 bool is_boolean() const
340 return base_type
== GLSL_TYPE_BOOL
;
344 * Query whether or not a type is a sampler
346 bool is_sampler() const
348 return base_type
== GLSL_TYPE_SAMPLER
;
352 * Query whether or not type is a sampler, or for struct and array
353 * types, contains a sampler.
355 bool contains_sampler() const;
358 * Get the Mesa texture target index for a sampler type.
360 gl_texture_index
sampler_index() const;
363 * Query whether or not a type is an array
365 bool is_array() const
367 return base_type
== GLSL_TYPE_ARRAY
;
371 * Query whether or not a type is a record
373 bool is_record() const
375 return base_type
== GLSL_TYPE_STRUCT
;
379 * Query whether or not a type is the void type singleton.
383 return base_type
== GLSL_TYPE_VOID
;
387 * Query whether or not a type is the error type singleton.
389 bool is_error() const
391 return base_type
== GLSL_TYPE_ERROR
;
395 * Query the full type of a matrix row
398 * If the type is not a matrix, \c glsl_type::error_type is returned.
399 * Otherwise a type matching the rows of the matrix is returned.
401 const glsl_type
*row_type() const
404 ? get_instance(base_type
, matrix_columns
, 1)
409 * Query the full type of a matrix column
412 * If the type is not a matrix, \c glsl_type::error_type is returned.
413 * Otherwise a type matching the columns of the matrix is returned.
415 const glsl_type
*column_type() const
418 ? get_instance(base_type
, vector_elements
, 1)
424 * Get the type of a structure field
427 * Pointer to the type of the named field. If the type is not a structure
428 * or the named field does not exist, \c glsl_type::error_type is returned.
430 const glsl_type
*field_type(const char *name
) const;
434 * Get the location of a filed within a record type
436 int field_index(const char *name
) const;
440 * Query the number of elements in an array type
443 * The number of elements in the array for array types or -1 for non-array
444 * types. If the number of elements in the array has not yet been declared,
447 int array_size() const
449 return is_array() ? length
: -1;
454 * ralloc context for all glsl_type allocations
456 * Set on the first call to \c glsl_type::new.
458 static void *mem_ctx
;
460 void init_ralloc_type_ctx(void);
462 /** Constructor for vector and matrix types */
463 glsl_type(GLenum gl_type
,
464 glsl_base_type base_type
, unsigned vector_elements
,
465 unsigned matrix_columns
, const char *name
);
467 /** Constructor for sampler types */
468 glsl_type(GLenum gl_type
,
469 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
470 unsigned type
, const char *name
);
472 /** Constructor for record types */
473 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
476 /** Constructor for array types */
477 glsl_type(const glsl_type
*array
, unsigned length
);
479 /** Hash table containing the known array types. */
480 static struct hash_table
*array_types
;
482 /** Hash table containing the known record types. */
483 static struct hash_table
*record_types
;
485 static int record_key_compare(const void *a
, const void *b
);
486 static unsigned record_key_hash(const void *key
);
489 * \name Pointers to various type singletons
492 static const glsl_type _error_type
;
493 static const glsl_type _void_type
;
494 static const glsl_type _sampler3D_type
;
495 static const glsl_type builtin_core_types
[];
496 static const glsl_type builtin_structure_types
[];
497 static const glsl_type builtin_110_deprecated_structure_types
[];
498 static const glsl_type builtin_110_types
[];
499 static const glsl_type builtin_120_types
[];
500 static const glsl_type builtin_130_types
[];
501 static const glsl_type builtin_ARB_texture_rectangle_types
[];
502 static const glsl_type builtin_EXT_texture_array_types
[];
503 static const glsl_type builtin_EXT_texture_buffer_object_types
[];
504 static const glsl_type builtin_OES_EGL_image_external_types
[];
508 * \name Methods to populate a symbol table with built-in types.
511 * This is one of the truely annoying things about C++. Methods that are
512 * completely internal and private to a type still have to be advertised to
513 * the world in a public header file.
516 static void generate_100ES_types(glsl_symbol_table
*);
517 static void generate_110_types(glsl_symbol_table
*);
518 static void generate_120_types(glsl_symbol_table
*);
519 static void generate_130_types(glsl_symbol_table
*);
520 static void generate_ARB_texture_rectangle_types(glsl_symbol_table
*, bool);
521 static void generate_EXT_texture_array_types(glsl_symbol_table
*, bool);
522 static void generate_OES_texture_3D_types(glsl_symbol_table
*, bool);
523 static void generate_OES_EGL_image_external_types(glsl_symbol_table
*, bool);
527 * \name Friend functions.
529 * These functions are friends because they must have C linkage and the
530 * need to call various private methods or access various private static
534 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
535 friend void _mesa_glsl_release_types(void);
539 struct glsl_struct_field
{
540 const struct glsl_type
*type
;
544 #endif /* __cplusplus */
546 #endif /* GLSL_TYPES_H */