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.
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);
58 enum glsl_sampler_dim
{
59 GLSL_SAMPLER_DIM_1D
= 0,
62 GLSL_SAMPLER_DIM_CUBE
,
63 GLSL_SAMPLER_DIM_RECT
,
70 glsl_base_type base_type
;
72 unsigned sampler_dimensionality
:3;
73 unsigned sampler_shadow
:1;
74 unsigned sampler_array
:1;
75 unsigned sampler_type
:2; /**< Type of data returned using this sampler.
76 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
77 * and \c GLSL_TYPE_UINT are valid.
80 /* Callers of this talloc-based new need not call delete. It's
81 * easier to just talloc_free 'mem_ctx' (or any of its ancestors). */
82 static void* operator new(size_t size
)
84 if (glsl_type::mem_ctx
== NULL
) {
85 glsl_type::mem_ctx
= talloc_init("glsl_type");
86 assert(glsl_type::mem_ctx
!= NULL
);
91 type
= talloc_size(glsl_type::mem_ctx
, size
);
97 /* If the user *does* call delete, that's OK, we will just
98 * talloc_free in that case. */
99 static void operator delete(void *type
)
105 * \name Vector and matrix element counts
107 * For scalars, each of these values will be 1. For non-numeric types
111 unsigned vector_elements
:3; /**< 1, 2, 3, or 4 vector elements. */
112 unsigned matrix_columns
:3; /**< 1, 2, 3, or 4 matrix columns. */
116 * Name of the data type
118 * This may be \c NULL for anonymous structures, for arrays, or for
124 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
125 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
126 * the number of values pointed to by \c fields.structure (below).
131 * Subtype of composite data types.
134 const struct glsl_type
*array
; /**< Type of array elements. */
135 const struct glsl_type
*parameters
; /**< Parameters to function. */
136 struct glsl_struct_field
*structure
; /**< List of struct fields. */
141 * \name Pointers to various public type singletons
144 static const glsl_type
*const error_type
;
145 static const glsl_type
*const void_type
;
146 static const glsl_type
*const int_type
;
147 static const glsl_type
*const ivec4_type
;
148 static const glsl_type
*const uint_type
;
149 static const glsl_type
*const uvec2_type
;
150 static const glsl_type
*const uvec3_type
;
151 static const glsl_type
*const uvec4_type
;
152 static const glsl_type
*const float_type
;
153 static const glsl_type
*const vec2_type
;
154 static const glsl_type
*const vec3_type
;
155 static const glsl_type
*const vec4_type
;
156 static const glsl_type
*const bool_type
;
157 static const glsl_type
*const mat2_type
;
158 static const glsl_type
*const mat2x3_type
;
159 static const glsl_type
*const mat2x4_type
;
160 static const glsl_type
*const mat3x2_type
;
161 static const glsl_type
*const mat3_type
;
162 static const glsl_type
*const mat3x4_type
;
163 static const glsl_type
*const mat4x2_type
;
164 static const glsl_type
*const mat4x3_type
;
165 static const glsl_type
*const mat4_type
;
170 * For numeric and boolean derrived types returns the basic scalar type
172 * If the type is a numeric or boolean scalar, vector, or matrix type,
173 * this function gets the scalar type of the individual components. For
174 * all other types, including arrays of numeric or boolean types, the
175 * error type is returned.
177 const glsl_type
*get_base_type() const;
180 * Query the type of elements in an array
183 * Pointer to the type of elements in the array for array types, or \c NULL
184 * for non-array types.
186 const glsl_type
*element_type() const
188 return is_array() ? fields
.array
: NULL
;
192 * Get the instance of a built-in scalar, vector, or matrix type
194 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
198 * Get the instance of an array type
200 static const glsl_type
*get_array_instance(const glsl_type
*base
,
204 * Get the instance of a record type
206 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
211 * Query the total number of scalars that make up a scalar, vector or matrix
213 unsigned components() const
215 return vector_elements
* matrix_columns
;
219 * Calculate the number of components slots required to hold this type
221 * This is used to determine how many uniform or varying locations a type
224 unsigned component_slots() const;
228 * Query whether or not a type is a scalar (non-vector and non-matrix).
230 bool is_scalar() const
232 return (vector_elements
== 1)
233 && (base_type
>= GLSL_TYPE_UINT
)
234 && (base_type
<= GLSL_TYPE_BOOL
);
238 * Query whether or not a type is a vector
240 bool is_vector() const
242 return (vector_elements
> 1)
243 && (matrix_columns
== 1)
244 && (base_type
>= GLSL_TYPE_UINT
)
245 && (base_type
<= GLSL_TYPE_BOOL
);
249 * Query whether or not a type is a matrix
251 bool is_matrix() const
253 /* GLSL only has float matrices. */
254 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
);
258 * Query whether or not a type is a non-array numeric type
260 bool is_numeric() const
262 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_FLOAT
);
266 * Query whether or not a type is an integral type
268 bool is_integer() const
270 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
274 * Query whether or not a type is a float type
276 bool is_float() const
278 return base_type
== GLSL_TYPE_FLOAT
;
282 * Query whether or not a type is a non-array boolean type
284 bool is_boolean() const
286 return base_type
== GLSL_TYPE_BOOL
;
290 * Query whether or not a type is a sampler
292 bool is_sampler() const
294 return base_type
== GLSL_TYPE_SAMPLER
;
298 * Query whether or not a type is an array
300 bool is_array() const
302 return base_type
== GLSL_TYPE_ARRAY
;
306 * Query whether or not a type is a record
308 bool is_record() const
310 return base_type
== GLSL_TYPE_STRUCT
;
314 * Query whether or not a type is the void type singleton.
318 return base_type
== GLSL_TYPE_VOID
;
322 * Query whether or not a type is the error type singleton.
324 bool is_error() const
326 return base_type
== GLSL_TYPE_ERROR
;
330 * Query the full type of a matrix row
333 * If the type is not a matrix, \c glsl_type::error_type is returned.
334 * Otherwise a type matching the rows of the matrix is returned.
336 const glsl_type
*row_type() const
339 ? get_instance(base_type
, matrix_columns
, 1)
344 * Query the full type of a matrix column
347 * If the type is not a matrix, \c glsl_type::error_type is returned.
348 * Otherwise a type matching the columns of the matrix is returned.
350 const glsl_type
*column_type() const
353 ? get_instance(base_type
, vector_elements
, 1)
359 * Get the type of a structure field
362 * Pointer to the type of the named field. If the type is not a structure
363 * or the named field does not exist, \c glsl_type::error_type is returned.
365 const glsl_type
*field_type(const char *name
) const;
369 * Get the location of a filed within a record type
371 int field_index(const char *name
) const;
375 * Query the number of elements in an array type
378 * The number of elements in the array for array types or -1 for non-array
379 * types. If the number of elements in the array has not yet been declared,
382 int array_size() const
384 return is_array() ? length
: -1;
389 * talloc context for all glsl_type allocations
391 * Set on the first call to \c glsl_type::new.
393 static void *mem_ctx
;
395 void init_talloc_type_ctx(void);
397 /** Constructor for vector and matrix types */
398 glsl_type(GLenum gl_type
,
399 glsl_base_type base_type
, unsigned vector_elements
,
400 unsigned matrix_columns
, const char *name
);
402 /** Constructor for sampler types */
403 glsl_type(GLenum gl_type
,
404 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
405 unsigned type
, const char *name
);
407 /** Constructor for record types */
408 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
411 /** Constructor for array types */
412 glsl_type(const glsl_type
*array
, unsigned length
);
414 /** Hash table containing the known array types. */
415 static struct hash_table
*array_types
;
417 /** Hash table containing the known record types. */
418 static struct hash_table
*record_types
;
420 static int record_key_compare(const void *a
, const void *b
);
421 static unsigned record_key_hash(const void *key
);
424 * \name Pointers to various type singletons
427 static const glsl_type _error_type
;
428 static const glsl_type _void_type
;
429 static const glsl_type builtin_core_types
[];
430 static const glsl_type builtin_structure_types
[];
431 static const glsl_type builtin_110_deprecated_structure_types
[];
432 static const glsl_type builtin_110_types
[];
433 static const glsl_type builtin_120_types
[];
434 static const glsl_type builtin_130_types
[];
435 static const glsl_type builtin_ARB_texture_rectangle_types
[];
436 static const glsl_type builtin_EXT_texture_array_types
[];
437 static const glsl_type builtin_EXT_texture_buffer_object_types
[];
441 * \name Methods to populate a symbol table with built-in types.
444 * This is one of the truely annoying things about C++. Methods that are
445 * completely internal and private to a type still have to be advertised to
446 * the world in a public header file.
449 static void generate_100ES_types(glsl_symbol_table
*);
450 static void generate_110_types(glsl_symbol_table
*);
451 static void generate_120_types(glsl_symbol_table
*);
452 static void generate_130_types(glsl_symbol_table
*);
453 static void generate_ARB_texture_rectangle_types(glsl_symbol_table
*, bool);
454 static void generate_EXT_texture_array_types(glsl_symbol_table
*, bool);
458 * \name Friend functions.
460 * These functions are friends because they must have C linkage and the
461 * need to call various private methods or access various private static
465 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
466 friend void _mesa_glsl_release_types(void);
470 struct glsl_struct_field
{
471 const struct glsl_type
*type
;
475 #endif /* GLSL_TYPES_H */