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 #define GLSL_TYPE_UINT 0
38 #define GLSL_TYPE_INT 1
39 #define GLSL_TYPE_FLOAT 2
40 #define GLSL_TYPE_BOOL 3
41 #define GLSL_TYPE_SAMPLER 4
42 #define GLSL_TYPE_STRUCT 5
43 #define GLSL_TYPE_ARRAY 6
44 #define GLSL_TYPE_FUNCTION 7
45 #define GLSL_TYPE_VOID 8
46 #define GLSL_TYPE_ERROR 9
48 enum glsl_sampler_dim
{
49 GLSL_SAMPLER_DIM_1D
= 0,
52 GLSL_SAMPLER_DIM_CUBE
,
53 GLSL_SAMPLER_DIM_RECT
,
62 unsigned sampler_dimensionality
:3;
63 unsigned sampler_shadow
:1;
64 unsigned sampler_array
:1;
65 unsigned sampler_type
:2; /**< Type of data returned using this sampler.
66 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
67 * and \c GLSL_TYPE_UINT are valid.
70 /* Callers of this talloc-based new need not call delete. It's
71 * easier to just talloc_free 'ctx' (or any of its ancestors). */
72 static void* operator new(size_t size
, void *ctx
)
76 type
= talloc_size(ctx
, size
);
82 /* If the user *does* call delete, that's OK, we will just
83 * talloc_free in that case. */
84 static void operator delete(void *type
)
90 * \name Vector and matrix element counts
92 * For scalars, each of these values will be 1. For non-numeric types
96 unsigned vector_elements
:3; /**< 1, 2, 3, or 4 vector elements. */
97 unsigned matrix_columns
:3; /**< 1, 2, 3, or 4 matrix columns. */
101 * Name of the data type
103 * This may be \c NULL for anonymous structures, for arrays, or for
109 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
110 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
111 * the number of values pointed to by \c fields.structure (below).
113 * For \c GLSL_TYPE_FUNCTION, it is the number of parameters to the
114 * function. The return value from a function is implicitly the first
115 * parameter. The types of the parameters are stored in
116 * \c fields.parameters (below).
121 * Subtype of composite data types.
124 const struct glsl_type
*array
; /**< Type of array elements. */
125 const struct glsl_type
*parameters
; /**< Parameters to function. */
126 const struct glsl_struct_field
*structure
;/**< List of struct fields. */
131 * \name Pointers to various public type singletons
134 static const glsl_type
*const error_type
;
135 static const glsl_type
*const int_type
;
136 static const glsl_type
*const ivec4_type
;
137 static const glsl_type
*const uint_type
;
138 static const glsl_type
*const uvec4_type
;
139 static const glsl_type
*const float_type
;
140 static const glsl_type
*const vec2_type
;
141 static const glsl_type
*const vec3_type
;
142 static const glsl_type
*const vec4_type
;
143 static const glsl_type
*const bool_type
;
144 static const glsl_type
*const mat2_type
;
145 static const glsl_type
*const mat2x3_type
;
146 static const glsl_type
*const mat2x4_type
;
147 static const glsl_type
*const mat3x2_type
;
148 static const glsl_type
*const mat3_type
;
149 static const glsl_type
*const mat3x4_type
;
150 static const glsl_type
*const mat4x2_type
;
151 static const glsl_type
*const mat4x3_type
;
152 static const glsl_type
*const mat4_type
;
156 glsl_type(GLenum gl_type
,
157 unsigned base_type
, unsigned vector_elements
,
158 unsigned matrix_columns
, const char *name
) :
160 base_type(base_type
),
161 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
163 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
167 /* Neither dimension is zero or both dimensions are zero.
169 assert((vector_elements
== 0) == (matrix_columns
== 0));
170 memset(& fields
, 0, sizeof(fields
));
173 glsl_type(GLenum gl_type
,
174 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
175 unsigned type
, const char *name
) :
177 base_type(GLSL_TYPE_SAMPLER
),
178 sampler_dimensionality(dim
), sampler_shadow(shadow
),
179 sampler_array(array
), sampler_type(type
),
180 vector_elements(0), matrix_columns(0),
184 memset(& fields
, 0, sizeof(fields
));
187 glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
189 base_type(GLSL_TYPE_STRUCT
),
190 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
192 vector_elements(0), matrix_columns(0),
196 this->fields
.structure
= fields
;
200 * For numeric and boolean derrived types returns the basic scalar type
202 * If the type is a numeric or boolean scalar, vector, or matrix type,
203 * this function gets the scalar type of the individual components. For
204 * all other types, including arrays of numeric or boolean types, the
205 * error type is returned.
207 const glsl_type
*get_base_type() const;
210 * Query the type of elements in an array
213 * Pointer to the type of elements in the array for array types, or \c NULL
214 * for non-array types.
216 const glsl_type
*element_type() const
218 return is_array() ? fields
.array
: NULL
;
222 * Get the instance of a built-in scalar, vector, or matrix type
224 static const glsl_type
*get_instance(unsigned base_type
, unsigned rows
,
228 * Get the instance of an array type
230 static const glsl_type
*get_array_instance(void *ctx
,
231 const glsl_type
*base
,
235 * Generate the constructor for this type and add it to the symbol table
237 class ir_function
*generate_constructor(class glsl_symbol_table
*) const;
240 * Query the total number of scalars that make up a scalar, vector or matrix
242 unsigned components() const
244 return vector_elements
* matrix_columns
;
248 * Calculate the number of components slots required to hold this type
250 * This is used to determine how many uniform or varying locations a type
253 unsigned component_slots() const;
257 * Query whether or not a type is a scalar (non-vector and non-matrix).
259 bool is_scalar() const
261 return (vector_elements
== 1)
262 && (base_type
>= GLSL_TYPE_UINT
)
263 && (base_type
<= GLSL_TYPE_BOOL
);
267 * Query whether or not a type is a vector
269 bool is_vector() const
271 return (vector_elements
> 1)
272 && (matrix_columns
== 1)
273 && (base_type
>= GLSL_TYPE_UINT
)
274 && (base_type
<= GLSL_TYPE_BOOL
);
278 * Query whether or not a type is a matrix
280 bool is_matrix() const
282 /* GLSL only has float matrices. */
283 return (matrix_columns
> 1) && (base_type
== GLSL_TYPE_FLOAT
);
287 * Query whether or not a type is a non-array numeric type
289 bool is_numeric() const
291 return (base_type
>= GLSL_TYPE_UINT
) && (base_type
<= GLSL_TYPE_FLOAT
);
295 * Query whether or not a type is an integral type
297 bool is_integer() const
299 return (base_type
== GLSL_TYPE_UINT
) || (base_type
== GLSL_TYPE_INT
);
303 * Query whether or not a type is a float type
305 bool is_float() const
307 return base_type
== GLSL_TYPE_FLOAT
;
311 * Query whether or not a type is a non-array boolean type
313 bool is_boolean() const
315 return base_type
== GLSL_TYPE_BOOL
;
319 * Query whether or not a type is a sampler
321 bool is_sampler() const
323 return base_type
== GLSL_TYPE_SAMPLER
;
327 * Query whether or not a type is an array
329 bool is_array() const
331 return base_type
== GLSL_TYPE_ARRAY
;
335 * Query whether or not a type is a record
337 bool is_record() const
339 return base_type
== GLSL_TYPE_STRUCT
;
343 * Query whether or not a type is the void type singleton.
347 return base_type
== GLSL_TYPE_VOID
;
351 * Query whether or not a type is the error type singleton.
353 bool is_error() const
355 return base_type
== GLSL_TYPE_ERROR
;
359 * Query the full type of a matrix row
362 * If the type is not a matrix, \c glsl_type::error_type is returned.
363 * Otherwise a type matching the rows of the matrix is returned.
365 const glsl_type
*row_type() const
368 ? get_instance(base_type
, matrix_columns
, 1)
373 * Query the full type of a matrix column
376 * If the type is not a matrix, \c glsl_type::error_type is returned.
377 * Otherwise a type matching the columns of the matrix is returned.
379 const glsl_type
*column_type() const
382 ? get_instance(base_type
, vector_elements
, 1)
388 * Get the type of a structure field
391 * Pointer to the type of the named field. If the type is not a structure
392 * or the named field does not exist, \c glsl_type::error_type is returned.
394 const glsl_type
*field_type(const char *name
) const;
398 * Get the location of a filed within a record type
400 int field_index(const char *name
) const;
404 * Query the number of elements in an array type
407 * The number of elements in the array for array types or -1 for non-array
408 * types. If the number of elements in the array has not yet been declared,
411 int array_size() const
413 return is_array() ? length
: -1;
418 * Constructor for array types
420 glsl_type(void *ctx
, const glsl_type
*array
, unsigned length
);
422 /** Hash table containing the known array types. */
423 static struct hash_table
*array_types
;
425 static int array_key_compare(const void *a
, const void *b
);
426 static unsigned array_key_hash(const void *key
);
429 struct glsl_struct_field
{
430 const struct glsl_type
*type
;
434 struct _mesa_glsl_parse_state
;
441 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
);
444 _mesa_glsl_initialize_constructors(struct exec_list
*instructions
,
445 struct _mesa_glsl_parse_state
*state
);
451 #endif /* GLSL_TYPES_H */