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.
32 #define GLSL_TYPE_UINT 0
33 #define GLSL_TYPE_INT 1
34 #define GLSL_TYPE_FLOAT 2
35 #define GLSL_TYPE_BOOL 3
36 #define GLSL_TYPE_SAMPLER 4
37 #define GLSL_TYPE_STRUCT 5
38 #define GLSL_TYPE_ARRAY 6
39 #define GLSL_TYPE_FUNCTION 7
40 #define GLSL_TYPE_VOID 8
41 #define GLSL_TYPE_ERROR 9
43 enum glsl_sampler_dim {
44 GLSL_SAMPLER_DIM_1D = 0,
47 GLSL_SAMPLER_DIM_CUBE,
48 GLSL_SAMPLER_DIM_RECT,
56 unsigned sampler_dimensionality:3;
57 unsigned sampler_shadow:1;
58 unsigned sampler_array:1;
59 unsigned sampler_type:2; /**< Type of data returned using this sampler.
60 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
61 * and \c GLSL_TYPE_UINT are valid.
65 * \name Vector and matrix element counts
67 * For scalars, each of these values will be 1. For non-numeric types
71 unsigned vector_elements:3; /**< 1, 2, 3, or 4 vector elements. */
72 unsigned matrix_columns:3; /**< 1, 2, 3, or 4 matrix columns. */
76 * Name of the data type
78 * This may be \c NULL for anonymous structures, for arrays, or for
84 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
85 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
86 * the number of values pointed to by \c fields.structure (below).
88 * For \c GLSL_TYPE_FUNCTION, it is the number of parameters to the
89 * function. The return value from a function is implicitly the first
90 * parameter. The types of the parameters are stored in
91 * \c fields.parameters (below).
96 * Subtype of composite data types.
99 const struct glsl_type *array; /**< Type of array elements. */
100 const struct glsl_type *parameters; /**< Parameters to function. */
101 const struct glsl_struct_field *structure;/**< List of struct fields. */
106 * \name Pointers to various public type singletons
109 static const glsl_type *const error_type;
110 static const glsl_type *const int_type;
111 static const glsl_type *const uint_type;
112 static const glsl_type *const float_type;
113 static const glsl_type *const vec2_type;
114 static const glsl_type *const vec3_type;
115 static const glsl_type *const vec4_type;
116 static const glsl_type *const bool_type;
117 static const glsl_type *const mat2_type;
118 static const glsl_type *const mat2x3_type;
119 static const glsl_type *const mat2x4_type;
120 static const glsl_type *const mat3x2_type;
121 static const glsl_type *const mat3_type;
122 static const glsl_type *const mat3x4_type;
123 static const glsl_type *const mat4x2_type;
124 static const glsl_type *const mat4x3_type;
125 static const glsl_type *const mat4_type;
129 glsl_type(unsigned base_type, unsigned vector_elements,
130 unsigned matrix_columns, const char *name) :
131 base_type(base_type),
132 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
134 vector_elements(vector_elements), matrix_columns(matrix_columns),
138 /* Neither dimension is zero or both dimensions are zero.
140 assert((vector_elements == 0) == (matrix_columns == 0));
141 memset(& fields, 0, sizeof(fields));
144 glsl_type(enum glsl_sampler_dim dim, bool shadow, bool array,
145 unsigned type, const char *name) :
146 base_type(GLSL_TYPE_SAMPLER),
147 sampler_dimensionality(dim), sampler_shadow(shadow),
148 sampler_array(array), sampler_type(type),
149 vector_elements(0), matrix_columns(0),
153 memset(& fields, 0, sizeof(fields));
156 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
158 base_type(GLSL_TYPE_STRUCT),
159 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
161 vector_elements(0), matrix_columns(0),
165 this->fields.structure = fields;
169 * For numeric and boolean derrived types returns the basic scalar type
171 * If the type is a numeric or boolean scalar, vector, or matrix type,
172 * this function gets the scalar type of the individual components. For
173 * all other types, including arrays of numeric or boolean types, the
174 * error type is returned.
176 const glsl_type *get_base_type() const;
179 * Query the type of elements in an array
182 * Pointer to the type of elements in the array for array types, or \c NULL
183 * for non-array types.
185 const glsl_type *element_type() const
187 return is_array() ? fields.array : NULL;
191 * Get the instance of a built-in scalar, vector, or matrix type
193 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
197 * Get the instance of an array type
199 static const glsl_type *get_array_instance(const glsl_type *base,
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 * Query whether or not a type is a scalar (non-vector and non-matrix).
218 bool is_scalar() const
220 return (vector_elements == 1)
221 && (base_type >= GLSL_TYPE_UINT)
222 && (base_type <= GLSL_TYPE_BOOL);
226 * Query whether or not a type is a vector
228 bool is_vector() const
230 return (vector_elements > 1)
231 && (matrix_columns == 1)
232 && (base_type >= GLSL_TYPE_UINT)
233 && (base_type <= GLSL_TYPE_BOOL);
237 * Query whether or not a type is a matrix
239 bool is_matrix() const
241 /* GLSL only has float matrices. */
242 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT);
246 * Query whether or not a type is a non-array numeric type
248 bool is_numeric() const
250 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT);
254 * Query whether or not a type is an integral type
256 bool is_integer() const
258 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
262 * Query whether or not a type is a float type
264 bool is_float() const
266 return base_type == GLSL_TYPE_FLOAT;
270 * Query whether or not a type is a non-array boolean type
272 bool is_boolean() const
274 return base_type == GLSL_TYPE_BOOL;
278 * Query whether or not a type is a sampler
280 bool is_sampler() const
282 return base_type == GLSL_TYPE_SAMPLER;
286 * Query whether or not a type is an array
288 bool is_array() const
290 return base_type == GLSL_TYPE_ARRAY;
294 * Query whether or not a type is the void type singleton.
298 return base_type == GLSL_TYPE_VOID;
302 * Query whether or not a type is the error type singleton.
304 bool is_error() const
306 return base_type == GLSL_TYPE_ERROR;
310 * Query the full type of a matrix row
313 * If the type is not a matrix, \c glsl_type::error_type is returned.
314 * Otherwise a type matching the rows of the matrix is returned.
316 const glsl_type *row_type() const
319 ? get_instance(base_type, matrix_columns, 1)
324 * Query the full type of a matrix column
327 * If the type is not a matrix, \c glsl_type::error_type is returned.
328 * Otherwise a type matching the columns of the matrix is returned.
330 const glsl_type *column_type() const
333 ? get_instance(base_type, vector_elements, 1)
339 * Get the type of a structure field
342 * Pointer to the type of the named field. If the type is not a structure
343 * or the named field does not exist, \c glsl_type::error_type is returned.
345 const glsl_type *field_type(const char *name) const;
349 * Query the number of elements in an array type
352 * The number of elements in the array for array types or -1 for non-array
353 * types. If the number of elements in the array has not yet been declared,
356 int array_size() const
358 return is_array() ? length : -1;
363 * Constructor for array types
365 glsl_type(const glsl_type *array, unsigned length);
367 /** Hash table containing the known array types. */
368 static struct hash_table *array_types;
370 static int array_key_compare(const void *a, const void *b);
371 static unsigned array_key_hash(const void *key);
374 struct glsl_struct_field {
375 const struct glsl_type *type;
379 struct _mesa_glsl_parse_state;
386 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
389 _mesa_glsl_initialize_constructors(struct exec_list *instructions,
390 struct _mesa_glsl_parse_state *state);
396 #endif /* GLSL_TYPES_H */