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
;
40 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
);
42 #define GLSL_TYPE_UINT 0
43 #define GLSL_TYPE_INT 1
44 #define GLSL_TYPE_FLOAT 2
45 #define GLSL_TYPE_BOOL 3
46 #define GLSL_TYPE_SAMPLER 4
47 #define GLSL_TYPE_STRUCT 5
48 #define GLSL_TYPE_ARRAY 6
49 #define GLSL_TYPE_FUNCTION 7
50 #define GLSL_TYPE_VOID 8
51 #define GLSL_TYPE_ERROR 9
53 enum glsl_sampler_dim
{
54 GLSL_SAMPLER_DIM_1D
= 0,
57 GLSL_SAMPLER_DIM_CUBE
,
58 GLSL_SAMPLER_DIM_RECT
,
67 unsigned sampler_dimensionality
:3;
68 unsigned sampler_shadow
:1;
69 unsigned sampler_array
:1;
70 unsigned sampler_type
:2; /**< Type of data returned using this sampler.
71 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
72 * and \c GLSL_TYPE_UINT are valid.
75 /* Callers of this talloc-based new need not call delete. It's
76 * easier to just talloc_free 'ctx' (or any of its ancestors). */
77 static void* operator new(size_t size
, void *ctx
)
81 type
= talloc_size(ctx
, size
);
87 /* If the user *does* call delete, that's OK, we will just
88 * talloc_free in that case. */
89 static void operator delete(void *type
)
95 * \name Vector and matrix element counts
97 * For scalars, each of these values will be 1. For non-numeric types
101 unsigned vector_elements
:3; /**< 1, 2, 3, or 4 vector elements. */
102 unsigned matrix_columns
:3; /**< 1, 2, 3, or 4 matrix columns. */
106 * Name of the data type
108 * This may be \c NULL for anonymous structures, for arrays, or for
114 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
115 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
116 * the number of values pointed to by \c fields.structure (below).
118 * For \c GLSL_TYPE_FUNCTION, it is the number of parameters to the
119 * function. The return value from a function is implicitly the first
120 * parameter. The types of the parameters are stored in
121 * \c fields.parameters (below).
126 * Subtype of composite data types.
129 const struct glsl_type
*array
; /**< Type of array elements. */
130 const struct glsl_type
*parameters
; /**< Parameters to function. */
131 const struct glsl_struct_field
*structure
;/**< List of struct fields. */
136 * \name Pointers to various public type singletons
139 static const glsl_type
*const error_type
;
140 static const glsl_type
*const int_type
;
141 static const glsl_type
*const ivec4_type
;
142 static const glsl_type
*const uint_type
;
143 static const glsl_type
*const uvec4_type
;
144 static const glsl_type
*const float_type
;
145 static const glsl_type
*const vec2_type
;
146 static const glsl_type
*const vec3_type
;
147 static const glsl_type
*const vec4_type
;
148 static const glsl_type
*const bool_type
;
149 static const glsl_type
*const mat2_type
;
150 static const glsl_type
*const mat2x3_type
;
151 static const glsl_type
*const mat2x4_type
;
152 static const glsl_type
*const mat3x2_type
;
153 static const glsl_type
*const mat3_type
;
154 static const glsl_type
*const mat3x4_type
;
155 static const glsl_type
*const mat4x2_type
;
156 static const glsl_type
*const mat4x3_type
;
157 static const glsl_type
*const mat4_type
;
162 * For numeric and boolean derrived types returns the basic scalar type
164 * If the type is a numeric or boolean scalar, vector, or matrix type,
165 * this function gets the scalar type of the individual components. For
166 * all other types, including arrays of numeric or boolean types, the
167 * error type is returned.
169 const glsl_type
*get_base_type() const;
172 * Query the type of elements in an array
175 * Pointer to the type of elements in the array for array types, or \c NULL
176 * for non-array types.
178 const glsl_type
*element_type() const
180 return is_array() ? fields
.array
: NULL
;
184 * Get the instance of a built-in scalar, vector, or matrix type
186 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
190 * Get the instance of an array type
192 static const glsl_type
*get_array_instance(void *ctx
,
193 const glsl_type
*base
,
197 * Get the instance of a record type
199 static const glsl_type
*get_record_instance(const glsl_struct_field
*fields
,
203 * Generate the constructor for this type and add it to the symbol table
205 class ir_function
*generate_constructor(class glsl_symbol_table
*) const;
208 * Query the total number of scalars that make up a scalar, vector or matrix
210 unsigned components() const
212 return vector_elements
* matrix_columns
;
216 * Calculate the number of components slots required to hold this type
218 * This is used to determine how many uniform or varying locations a type
221 unsigned component_slots() const;
225 * Query whether or not a type is a scalar (non-vector and non-matrix).
227 bool is_scalar() const
229 return (vector_elements
== 1)
230 && (base_type
>= GLSL_TYPE_UINT
)
231 && (base_type
<= GLSL_TYPE_BOOL
);
235 * Query whether or not a type is a vector
237 bool is_vector() const
239 return (vector_elements
> 1)
240 && (matrix_columns
== 1)
241 && (base_type
>= GLSL_TYPE_UINT
)
242 && (base_type
<= GLSL_TYPE_BOOL
);
246 * Query whether or not a type is a matrix
248 bool is_matrix() const
250 /* GLSL only has float matrices. */
251 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
);
255 * Query whether or not a type is a non-array numeric type
257 bool is_numeric() const
259 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_FLOAT
);
263 * Query whether or not a type is an integral type
265 bool is_integer() const
267 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
271 * Query whether or not a type is a float type
273 bool is_float() const
275 return base_type
== GLSL_TYPE_FLOAT
;
279 * Query whether or not a type is a non-array boolean type
281 bool is_boolean() const
283 return base_type
== GLSL_TYPE_BOOL
;
287 * Query whether or not a type is a sampler
289 bool is_sampler() const
291 return base_type
== GLSL_TYPE_SAMPLER
;
295 * Query whether or not a type is an array
297 bool is_array() const
299 return base_type
== GLSL_TYPE_ARRAY
;
303 * Query whether or not a type is a record
305 bool is_record() const
307 return base_type
== GLSL_TYPE_STRUCT
;
311 * Query whether or not a type is the void type singleton.
315 return base_type
== GLSL_TYPE_VOID
;
319 * Query whether or not a type is the error type singleton.
321 bool is_error() const
323 return base_type
== GLSL_TYPE_ERROR
;
327 * Query the full type of a matrix row
330 * If the type is not a matrix, \c glsl_type::error_type is returned.
331 * Otherwise a type matching the rows of the matrix is returned.
333 const glsl_type
*row_type() const
336 ? get_instance(base_type
, matrix_columns
, 1)
341 * Query the full type of a matrix column
344 * If the type is not a matrix, \c glsl_type::error_type is returned.
345 * Otherwise a type matching the columns of the matrix is returned.
347 const glsl_type
*column_type() const
350 ? get_instance(base_type
, vector_elements
, 1)
356 * Get the type of a structure field
359 * Pointer to the type of the named field. If the type is not a structure
360 * or the named field does not exist, \c glsl_type::error_type is returned.
362 const glsl_type
*field_type(const char *name
) const;
366 * Get the location of a filed within a record type
368 int field_index(const char *name
) const;
372 * Query the number of elements in an array type
375 * The number of elements in the array for array types or -1 for non-array
376 * types. If the number of elements in the array has not yet been declared,
379 int array_size() const
381 return is_array() ? length
: -1;
385 /** Constructor for vector and matrix types */
386 glsl_type(GLenum gl_type
,
387 unsigned base_type
, unsigned vector_elements
,
388 unsigned matrix_columns
, const char *name
);
390 /** Constructor for sampler types */
391 glsl_type(GLenum gl_type
,
392 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
393 unsigned type
, const char *name
);
395 /** Constructor for record types */
396 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
399 /** Constructor for array types */
400 glsl_type(void *ctx
, const glsl_type
*array
, unsigned length
);
402 /** Hash table containing the known array types. */
403 static struct hash_table
*array_types
;
405 static int array_key_compare(const void *a
, const void *b
);
406 static unsigned array_key_hash(const void *key
);
408 /** Hash table containing the known record types. */
409 static struct hash_table
*record_types
;
411 static int record_key_compare(const void *a
, const void *b
);
412 static unsigned record_key_hash(const void *key
);
415 * \name Pointers to various type singletons
418 static const glsl_type _error_type
;
419 static const glsl_type void_type
;
420 static const glsl_type builtin_core_types
[];
421 static const glsl_type builtin_structure_types
[];
422 static const glsl_type builtin_110_deprecated_structure_types
[];
423 static const glsl_type builtin_120_types
[];
424 static const glsl_type builtin_130_types
[];
425 static const glsl_type builtin_ARB_texture_rectangle_types
[];
426 static const glsl_type builtin_EXT_texture_array_types
[];
427 static const glsl_type builtin_EXT_texture_buffer_object_types
[];
431 * \name Methods to populate a symbol table with built-in types.
434 * This is one of the truely annoying things about C++. Methods that are
435 * completely internal and private to a type still have to be advertised to
436 * the world in a public header file.
439 static void generate_110_types(class glsl_symbol_table
*);
440 static void generate_120_types(class glsl_symbol_table
*);
441 static void generate_130_types(class glsl_symbol_table
*);
442 static void generate_ARB_texture_rectangle_types(class glsl_symbol_table
*,
444 static void generate_EXT_texture_array_types(class glsl_symbol_table
*,
447 * This function is a friend because it needs to call the various
448 * generate_*_types functions and it has C linkage.
450 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*);
454 struct glsl_struct_field
{
455 const struct glsl_type
*type
;
459 #endif /* GLSL_TYPES_H */