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