glsl2: Move temp declaration to correct side of if-statement in IR
[mesa.git] / src / glsl / glsl_types.h
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2009 Intel Corporation
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 #pragma once
26 #ifndef GLSL_TYPES_H
27 #define GLSL_TYPES_H
28
29 #include <cstring>
30 #include <cassert>
31
32 extern "C" {
33 #include "GL/gl.h"
34 #include <talloc.h>
35 }
36
37 struct _mesa_glsl_parse_state;
38
39 extern "C" void
40 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
41
42 extern "C" void
43 _mesa_glsl_release_types(void);
44
45 #define GLSL_TYPE_UINT 0
46 #define GLSL_TYPE_INT 1
47 #define GLSL_TYPE_FLOAT 2
48 #define GLSL_TYPE_BOOL 3
49 #define GLSL_TYPE_SAMPLER 4
50 #define GLSL_TYPE_STRUCT 5
51 #define GLSL_TYPE_ARRAY 6
52 #define GLSL_TYPE_FUNCTION 7
53 #define GLSL_TYPE_VOID 8
54 #define GLSL_TYPE_ERROR 9
55
56 enum glsl_sampler_dim {
57 GLSL_SAMPLER_DIM_1D = 0,
58 GLSL_SAMPLER_DIM_2D,
59 GLSL_SAMPLER_DIM_3D,
60 GLSL_SAMPLER_DIM_CUBE,
61 GLSL_SAMPLER_DIM_RECT,
62 GLSL_SAMPLER_DIM_BUF
63 };
64
65
66 struct glsl_type {
67 GLenum gl_type;
68 unsigned base_type:4;
69
70 unsigned sampler_dimensionality:3;
71 unsigned sampler_shadow:1;
72 unsigned sampler_array:1;
73 unsigned sampler_type:2; /**< Type of data returned using this sampler.
74 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
75 * and \c GLSL_TYPE_UINT are valid.
76 */
77
78 /* Callers of this talloc-based new need not call delete. It's
79 * easier to just talloc_free 'ctx' (or any of its ancestors). */
80 static void* operator new(size_t size)
81 {
82 if (glsl_type::ctx == NULL) {
83 glsl_type::ctx = talloc_init("glsl_type");
84 assert(glsl_type::ctx != NULL);
85 }
86
87 void *type;
88
89 type = talloc_size(glsl_type::ctx, size);
90 assert(type != NULL);
91
92 return type;
93 }
94
95 /* If the user *does* call delete, that's OK, we will just
96 * talloc_free in that case. */
97 static void operator delete(void *type)
98 {
99 talloc_free(type);
100 }
101
102 /**
103 * \name Vector and matrix element counts
104 *
105 * For scalars, each of these values will be 1. For non-numeric types
106 * these will be 0.
107 */
108 /*@{*/
109 unsigned vector_elements:3; /**< 1, 2, 3, or 4 vector elements. */
110 unsigned matrix_columns:3; /**< 1, 2, 3, or 4 matrix columns. */
111 /*@}*/
112
113 /**
114 * Name of the data type
115 *
116 * This may be \c NULL for anonymous structures, for arrays, or for
117 * function types.
118 */
119 const char *name;
120
121 /**
122 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
123 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
124 * the number of values pointed to by \c fields.structure (below).
125 *
126 * For \c GLSL_TYPE_FUNCTION, it is the number of parameters to the
127 * function. The return value from a function is implicitly the first
128 * parameter. The types of the parameters are stored in
129 * \c fields.parameters (below).
130 */
131 unsigned length;
132
133 /**
134 * Subtype of composite data types.
135 */
136 union {
137 const struct glsl_type *array; /**< Type of array elements. */
138 const struct glsl_type *parameters; /**< Parameters to function. */
139 const struct glsl_struct_field *structure;/**< List of struct fields. */
140 } fields;
141
142
143 /**
144 * \name Pointers to various public type singletons
145 */
146 /*@{*/
147 static const glsl_type *const error_type;
148 static const glsl_type *const int_type;
149 static const glsl_type *const ivec4_type;
150 static const glsl_type *const uint_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;
166 /*@}*/
167
168
169 /**
170 * For numeric and boolean derrived types returns the basic scalar type
171 *
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.
176 */
177 const glsl_type *get_base_type() const;
178
179 /**
180 * Query the type of elements in an array
181 *
182 * \return
183 * Pointer to the type of elements in the array for array types, or \c NULL
184 * for non-array types.
185 */
186 const glsl_type *element_type() const
187 {
188 return is_array() ? fields.array : NULL;
189 }
190
191 /**
192 * Get the instance of a built-in scalar, vector, or matrix type
193 */
194 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
195 unsigned columns);
196
197 /**
198 * Get the instance of an array type
199 */
200 static const glsl_type *get_array_instance(void *ctx,
201 const glsl_type *base,
202 unsigned elements);
203
204 /**
205 * Get the instance of a record type
206 */
207 static const glsl_type *get_record_instance(const glsl_struct_field *fields,
208 unsigned num_fields,
209 const char *name);
210 /**
211 * Generate the constructor for this type and add it to the symbol table
212 */
213 class ir_function *generate_constructor(class glsl_symbol_table *) const;
214
215 /**
216 * Query the total number of scalars that make up a scalar, vector or matrix
217 */
218 unsigned components() const
219 {
220 return vector_elements * matrix_columns;
221 }
222
223 /**
224 * Calculate the number of components slots required to hold this type
225 *
226 * This is used to determine how many uniform or varying locations a type
227 * might occupy.
228 */
229 unsigned component_slots() const;
230
231
232 /**
233 * Query whether or not a type is a scalar (non-vector and non-matrix).
234 */
235 bool is_scalar() const
236 {
237 return (vector_elements == 1)
238 && (base_type >= GLSL_TYPE_UINT)
239 && (base_type <= GLSL_TYPE_BOOL);
240 }
241
242 /**
243 * Query whether or not a type is a vector
244 */
245 bool is_vector() const
246 {
247 return (vector_elements > 1)
248 && (matrix_columns == 1)
249 && (base_type >= GLSL_TYPE_UINT)
250 && (base_type <= GLSL_TYPE_BOOL);
251 }
252
253 /**
254 * Query whether or not a type is a matrix
255 */
256 bool is_matrix() const
257 {
258 /* GLSL only has float matrices. */
259 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT);
260 }
261
262 /**
263 * Query whether or not a type is a non-array numeric type
264 */
265 bool is_numeric() const
266 {
267 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT);
268 }
269
270 /**
271 * Query whether or not a type is an integral type
272 */
273 bool is_integer() const
274 {
275 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
276 }
277
278 /**
279 * Query whether or not a type is a float type
280 */
281 bool is_float() const
282 {
283 return base_type == GLSL_TYPE_FLOAT;
284 }
285
286 /**
287 * Query whether or not a type is a non-array boolean type
288 */
289 bool is_boolean() const
290 {
291 return base_type == GLSL_TYPE_BOOL;
292 }
293
294 /**
295 * Query whether or not a type is a sampler
296 */
297 bool is_sampler() const
298 {
299 return base_type == GLSL_TYPE_SAMPLER;
300 }
301
302 /**
303 * Query whether or not a type is an array
304 */
305 bool is_array() const
306 {
307 return base_type == GLSL_TYPE_ARRAY;
308 }
309
310 /**
311 * Query whether or not a type is a record
312 */
313 bool is_record() const
314 {
315 return base_type == GLSL_TYPE_STRUCT;
316 }
317
318 /**
319 * Query whether or not a type is the void type singleton.
320 */
321 bool is_void() const
322 {
323 return base_type == GLSL_TYPE_VOID;
324 }
325
326 /**
327 * Query whether or not a type is the error type singleton.
328 */
329 bool is_error() const
330 {
331 return base_type == GLSL_TYPE_ERROR;
332 }
333
334 /**
335 * Query the full type of a matrix row
336 *
337 * \return
338 * If the type is not a matrix, \c glsl_type::error_type is returned.
339 * Otherwise a type matching the rows of the matrix is returned.
340 */
341 const glsl_type *row_type() const
342 {
343 return is_matrix()
344 ? get_instance(base_type, matrix_columns, 1)
345 : error_type;
346 }
347
348 /**
349 * Query the full type of a matrix column
350 *
351 * \return
352 * If the type is not a matrix, \c glsl_type::error_type is returned.
353 * Otherwise a type matching the columns of the matrix is returned.
354 */
355 const glsl_type *column_type() const
356 {
357 return is_matrix()
358 ? get_instance(base_type, vector_elements, 1)
359 : error_type;
360 }
361
362
363 /**
364 * Get the type of a structure field
365 *
366 * \return
367 * Pointer to the type of the named field. If the type is not a structure
368 * or the named field does not exist, \c glsl_type::error_type is returned.
369 */
370 const glsl_type *field_type(const char *name) const;
371
372
373 /**
374 * Get the location of a filed within a record type
375 */
376 int field_index(const char *name) const;
377
378
379 /**
380 * Query the number of elements in an array type
381 *
382 * \return
383 * The number of elements in the array for array types or -1 for non-array
384 * types. If the number of elements in the array has not yet been declared,
385 * zero is returned.
386 */
387 int array_size() const
388 {
389 return is_array() ? length : -1;
390 }
391
392 private:
393 /**
394 * talloc context for all glsl_type allocations
395 *
396 * Set on the first call to \c glsl_type::new.
397 */
398 static TALLOC_CTX *ctx;
399
400 /** Constructor for vector and matrix types */
401 glsl_type(GLenum gl_type,
402 unsigned base_type, unsigned vector_elements,
403 unsigned matrix_columns, const char *name);
404
405 /** Constructor for sampler types */
406 glsl_type(GLenum gl_type,
407 enum glsl_sampler_dim dim, bool shadow, bool array,
408 unsigned type, const char *name);
409
410 /** Constructor for record types */
411 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
412 const char *name);
413
414 /** Constructor for array types */
415 glsl_type(void *ctx, const glsl_type *array, unsigned length);
416
417 /** Hash table containing the known array types. */
418 static struct hash_table *array_types;
419
420 static int array_key_compare(const void *a, const void *b);
421 static unsigned array_key_hash(const void *key);
422
423 /** Hash table containing the known record types. */
424 static struct hash_table *record_types;
425
426 static int record_key_compare(const void *a, const void *b);
427 static unsigned record_key_hash(const void *key);
428
429 /**
430 * \name Pointers to various type singletons
431 */
432 /*@{*/
433 static const glsl_type _error_type;
434 static const glsl_type void_type;
435 static const glsl_type builtin_core_types[];
436 static const glsl_type builtin_structure_types[];
437 static const glsl_type builtin_110_deprecated_structure_types[];
438 static const glsl_type builtin_120_types[];
439 static const glsl_type builtin_130_types[];
440 static const glsl_type builtin_ARB_texture_rectangle_types[];
441 static const glsl_type builtin_EXT_texture_array_types[];
442 static const glsl_type builtin_EXT_texture_buffer_object_types[];
443 /*@}*/
444
445 /**
446 * \name Methods to populate a symbol table with built-in types.
447 *
448 * \internal
449 * This is one of the truely annoying things about C++. Methods that are
450 * completely internal and private to a type still have to be advertised to
451 * the world in a public header file.
452 */
453 /*@{*/
454 static void generate_110_types(class glsl_symbol_table *);
455 static void generate_120_types(class glsl_symbol_table *);
456 static void generate_130_types(class glsl_symbol_table *);
457 static void generate_ARB_texture_rectangle_types(class glsl_symbol_table *,
458 bool);
459 static void generate_EXT_texture_array_types(class glsl_symbol_table *,
460 bool);
461 /*@}*/
462
463 /**
464 * \name Friend functions.
465 *
466 * These functions are friends because they must have C linkage and the
467 * need to call various private methods or access various private static
468 * data.
469 */
470 /*@{*/
471 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
472 friend void _mesa_glsl_release_types(void);
473 /*@}*/
474 };
475
476 struct glsl_struct_field {
477 const struct glsl_type *type;
478 const char *name;
479 };
480
481 #endif /* GLSL_TYPES_H */