Tizen 2.0 Release
[profile/ivi/osmesa.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 <string.h>
30 #include <assert.h>
31
32 extern "C" {
33 #include "GL/gl.h"
34 }
35
36 #include "ralloc.h"
37
38 struct _mesa_glsl_parse_state;
39 struct glsl_symbol_table;
40
41 extern "C" void
42 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
43
44 extern "C" void
45 _mesa_glsl_release_types(void);
46
47 enum glsl_base_type {
48    GLSL_TYPE_UINT = 0,
49    GLSL_TYPE_INT,
50    GLSL_TYPE_FLOAT,
51    GLSL_TYPE_BOOL,
52    GLSL_TYPE_SAMPLER,
53    GLSL_TYPE_STRUCT,
54    GLSL_TYPE_ARRAY,
55    GLSL_TYPE_VOID,
56    GLSL_TYPE_ERROR
57 };
58
59 enum glsl_sampler_dim {
60    GLSL_SAMPLER_DIM_1D = 0,
61    GLSL_SAMPLER_DIM_2D,
62    GLSL_SAMPLER_DIM_3D,
63    GLSL_SAMPLER_DIM_CUBE,
64    GLSL_SAMPLER_DIM_RECT,
65    GLSL_SAMPLER_DIM_BUF
66 };
67
68
69 struct glsl_type {
70    GLenum gl_type;
71    glsl_base_type base_type;
72
73    unsigned sampler_dimensionality:3; /**< \see glsl_sampler_dim */
74    unsigned sampler_shadow:1;
75    unsigned sampler_array:1;
76    unsigned sampler_type:2;    /**< Type of data returned using this sampler.
77                                 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
78                                 * and \c GLSL_TYPE_UINT are valid.
79                                 */
80
81    /* Callers of this ralloc-based new need not call delete. It's
82     * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
83    static void* operator new(size_t size)
84    {
85       if (glsl_type::mem_ctx == NULL) {
86          glsl_type::mem_ctx = ralloc_context(NULL);
87          assert(glsl_type::mem_ctx != NULL);
88       }
89
90       void *type;
91
92       type = ralloc_size(glsl_type::mem_ctx, size);
93       assert(type != NULL);
94
95       return type;
96    }
97
98    /* If the user *does* call delete, that's OK, we will just
99     * ralloc_free in that case. */
100    static void operator delete(void *type)
101    {
102       ralloc_free(type);
103    }
104
105    /**
106     * \name Vector and matrix element counts
107     *
108     * For scalars, each of these values will be 1.  For non-numeric types
109     * these will be 0.
110     */
111    /*@{*/
112    unsigned vector_elements:3; /**< 1, 2, 3, or 4 vector elements. */
113    unsigned matrix_columns:3;  /**< 1, 2, 3, or 4 matrix columns. */
114    /*@}*/
115
116    /**
117     * Name of the data type
118     *
119     * This may be \c NULL for anonymous structures, for arrays, or for
120     * function types.
121     */
122    const char *name;
123
124    /**
125     * For \c GLSL_TYPE_ARRAY, this is the length of the array.  For
126     * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
127     * the number of values pointed to by \c fields.structure (below).
128     */
129    unsigned length;
130
131    /**
132     * Subtype of composite data types.
133     */
134    union {
135       const struct glsl_type *array;            /**< Type of array elements. */
136       const struct glsl_type *parameters;       /**< Parameters to function. */
137       struct glsl_struct_field *structure;      /**< List of struct fields. */
138    } fields;
139
140
141    /**
142     * \name Pointers to various public type singletons
143     */
144    /*@{*/
145    static const glsl_type *const error_type;
146    static const glsl_type *const void_type;
147    static const glsl_type *const int_type;
148    static const glsl_type *const ivec4_type;
149    static const glsl_type *const uint_type;
150    static const glsl_type *const uvec2_type;
151    static const glsl_type *const uvec3_type;
152    static const glsl_type *const uvec4_type;
153    static const glsl_type *const float_type;
154    static const glsl_type *const vec2_type;
155    static const glsl_type *const vec3_type;
156    static const glsl_type *const vec4_type;
157    static const glsl_type *const bool_type;
158    static const glsl_type *const mat2_type;
159    static const glsl_type *const mat2x3_type;
160    static const glsl_type *const mat2x4_type;
161    static const glsl_type *const mat3x2_type;
162    static const glsl_type *const mat3_type;
163    static const glsl_type *const mat3x4_type;
164    static const glsl_type *const mat4x2_type;
165    static const glsl_type *const mat4x3_type;
166    static const glsl_type *const mat4_type;
167    /*@}*/
168
169
170    /**
171     * For numeric and boolean derrived types returns the basic scalar type
172     *
173     * If the type is a numeric or boolean scalar, vector, or matrix type,
174     * this function gets the scalar type of the individual components.  For
175     * all other types, including arrays of numeric or boolean types, the
176     * error type is returned.
177     */
178    const glsl_type *get_base_type() const;
179
180    /**
181     * Query the type of elements in an array
182     *
183     * \return
184     * Pointer to the type of elements in the array for array types, or \c NULL
185     * for non-array types.
186     */
187    const glsl_type *element_type() const
188    {
189       return is_array() ? fields.array : NULL;
190    }
191
192    /**
193     * Get the instance of a built-in scalar, vector, or matrix type
194     */
195    static const glsl_type *get_instance(unsigned base_type, unsigned rows,
196                                         unsigned columns);
197
198    /**
199     * Get the instance of an array type
200     */
201    static const glsl_type *get_array_instance(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    /**
212     * Query the total number of scalars that make up a scalar, vector or matrix
213     */
214    unsigned components() const
215    {
216       return vector_elements * matrix_columns;
217    }
218
219    /**
220     * Calculate the number of components slots required to hold this type
221     *
222     * This is used to determine how many uniform or varying locations a type
223     * might occupy.
224     */
225    unsigned component_slots() const;
226
227    /**
228     * \brief Can this type be implicitly converted to another?
229     *
230     * \return True if the types are identical or if this type can be converted
231     *         to \c desired according to Section 4.1.10 of the GLSL spec.
232     *
233     * \verbatim
234     * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
235     * Implicit Conversions:
236     *
237     *     In some situations, an expression and its type will be implicitly
238     *     converted to a different type. The following table shows all allowed
239     *     implicit conversions:
240     *
241     *     Type of expression | Can be implicitly converted to
242     *     --------------------------------------------------
243     *     int                  float
244     *     uint
245     *
246     *     ivec2                vec2
247     *     uvec2
248     *
249     *     ivec3                vec3
250     *     uvec3
251     *
252     *     ivec4                vec4
253     *     uvec4
254     *
255     *     There are no implicit array or structure conversions. For example,
256     *     an array of int cannot be implicitly converted to an array of float.
257     *     There are no implicit conversions between signed and unsigned
258     *     integers.
259     * \endverbatim
260     */
261    bool can_implicitly_convert_to(const glsl_type *desired) const;
262
263    /**
264     * Query whether or not a type is a scalar (non-vector and non-matrix).
265     */
266    bool is_scalar() const
267    {
268       return (vector_elements == 1)
269          && (base_type >= GLSL_TYPE_UINT)
270          && (base_type <= GLSL_TYPE_BOOL);
271    }
272
273    /**
274     * Query whether or not a type is a vector
275     */
276    bool is_vector() const
277    {
278       return (vector_elements > 1)
279          && (matrix_columns == 1)
280          && (base_type >= GLSL_TYPE_UINT)
281          && (base_type <= GLSL_TYPE_BOOL);
282    }
283
284    /**
285     * Query whether or not a type is a matrix
286     */
287    bool is_matrix() const
288    {
289       /* GLSL only has float matrices. */
290       return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT);
291    }
292
293    /**
294     * Query whether or not a type is a non-array numeric type
295     */
296    bool is_numeric() const
297    {
298       return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT);
299    }
300
301    /**
302     * Query whether or not a type is an integral type
303     */
304    bool is_integer() const
305    {
306       return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
307    }
308
309    /**
310     * Query whether or not a type is a float type
311     */
312    bool is_float() const
313    {
314       return base_type == GLSL_TYPE_FLOAT;
315    }
316
317    /**
318     * Query whether or not a type is a non-array boolean type
319     */
320    bool is_boolean() const
321    {
322       return base_type == GLSL_TYPE_BOOL;
323    }
324
325    /**
326     * Query whether or not a type is a sampler
327     */
328    bool is_sampler() const
329    {
330       return base_type == GLSL_TYPE_SAMPLER;
331    }
332
333    /**
334     * Query whether or not type is a sampler, or for struct and array
335     * types, contains a sampler.
336     */
337    bool contains_sampler() const;
338
339    /**
340     * Query whether or not a type is an array
341     */
342    bool is_array() const
343    {
344       return base_type == GLSL_TYPE_ARRAY;
345    }
346
347    /**
348     * Query whether or not a type is a record
349     */
350    bool is_record() const
351    {
352       return base_type == GLSL_TYPE_STRUCT;
353    }
354
355    /**
356     * Query whether or not a type is the void type singleton.
357     */
358    bool is_void() const
359    {
360       return base_type == GLSL_TYPE_VOID;
361    }
362
363    /**
364     * Query whether or not a type is the error type singleton.
365     */
366    bool is_error() const
367    {
368       return base_type == GLSL_TYPE_ERROR;
369    }
370
371    /**
372     * Query the full type of a matrix row
373     *
374     * \return
375     * If the type is not a matrix, \c glsl_type::error_type is returned.
376     * Otherwise a type matching the rows of the matrix is returned.
377     */
378    const glsl_type *row_type() const
379    {
380       return is_matrix()
381          ? get_instance(base_type, matrix_columns, 1)
382          : error_type;
383    }
384
385    /**
386     * Query the full type of a matrix column
387     *
388     * \return
389     * If the type is not a matrix, \c glsl_type::error_type is returned.
390     * Otherwise a type matching the columns of the matrix is returned.
391     */
392    const glsl_type *column_type() const
393    {
394       return is_matrix()
395          ? get_instance(base_type, vector_elements, 1)
396          : error_type;
397    }
398
399
400    /**
401     * Get the type of a structure field
402     *
403     * \return
404     * Pointer to the type of the named field.  If the type is not a structure
405     * or the named field does not exist, \c glsl_type::error_type is returned.
406     */
407    const glsl_type *field_type(const char *name) const;
408
409
410    /**
411     * Get the location of a filed within a record type
412     */
413    int field_index(const char *name) const;
414
415
416    /**
417     * Query the number of elements in an array type
418     *
419     * \return
420     * The number of elements in the array for array types or -1 for non-array
421     * types.  If the number of elements in the array has not yet been declared,
422     * zero is returned.
423     */
424    int array_size() const
425    {
426       return is_array() ? length : -1;
427    }
428
429 private:
430    /**
431     * ralloc context for all glsl_type allocations
432     *
433     * Set on the first call to \c glsl_type::new.
434     */
435    static void *mem_ctx;
436
437    void init_ralloc_type_ctx(void);
438
439    /** Constructor for vector and matrix types */
440    glsl_type(GLenum gl_type,
441              glsl_base_type base_type, unsigned vector_elements,
442              unsigned matrix_columns, const char *name);
443
444    /** Constructor for sampler types */
445    glsl_type(GLenum gl_type,
446              enum glsl_sampler_dim dim, bool shadow, bool array,
447              unsigned type, const char *name);
448
449    /** Constructor for record types */
450    glsl_type(const glsl_struct_field *fields, unsigned num_fields,
451              const char *name);
452
453    /** Constructor for array types */
454    glsl_type(const glsl_type *array, unsigned length);
455
456    /** Hash table containing the known array types. */
457    static struct hash_table *array_types;
458
459    /** Hash table containing the known record types. */
460    static struct hash_table *record_types;
461
462    static int record_key_compare(const void *a, const void *b);
463    static unsigned record_key_hash(const void *key);
464
465    /**
466     * \name Pointers to various type singletons
467     */
468    /*@{*/
469    static const glsl_type _error_type;
470    static const glsl_type _void_type;
471    static const glsl_type _sampler3D_type;
472    static const glsl_type builtin_core_types[];
473    static const glsl_type builtin_structure_types[];
474    static const glsl_type builtin_110_deprecated_structure_types[];
475    static const glsl_type builtin_110_types[];
476    static const glsl_type builtin_120_types[];
477    static const glsl_type builtin_130_types[];
478    static const glsl_type builtin_ARB_texture_rectangle_types[];
479    static const glsl_type builtin_EXT_texture_array_types[];
480    static const glsl_type builtin_EXT_texture_buffer_object_types[];
481    /*@}*/
482
483    /**
484     * \name Methods to populate a symbol table with built-in types.
485     *
486     * \internal
487     * This is one of the truely annoying things about C++.  Methods that are
488     * completely internal and private to a type still have to be advertised to
489     * the world in a public header file.
490     */
491    /*@{*/
492    static void generate_100ES_types(glsl_symbol_table *);
493    static void generate_110_types(glsl_symbol_table *);
494    static void generate_120_types(glsl_symbol_table *);
495    static void generate_130_types(glsl_symbol_table *);
496    static void generate_ARB_texture_rectangle_types(glsl_symbol_table *, bool);
497    static void generate_EXT_texture_array_types(glsl_symbol_table *, bool);
498    static void generate_OES_texture_3D_types(glsl_symbol_table *, bool);
499    /*@}*/
500
501    /**
502     * \name Friend functions.
503     *
504     * These functions are friends because they must have C linkage and the
505     * need to call various private methods or access various private static
506     * data.
507     */
508    /*@{*/
509    friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
510    friend void _mesa_glsl_release_types(void);
511    /*@}*/
512 };
513
514 struct glsl_struct_field {
515    const struct glsl_type *type;
516    const char *name;
517 };
518
519 #endif /* GLSL_TYPES_H */