Home | History | Annotate | Download | only in compiler
      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 #ifdef __cplusplus
     33 extern "C" {
     34 #endif
     35 
     36 struct _mesa_glsl_parse_state;
     37 struct glsl_symbol_table;
     38 
     39 extern void
     40 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
     41 
     42 extern void
     43 _mesa_glsl_release_types(void);
     44 
     45 #ifdef __cplusplus
     46 }
     47 #endif
     48 
     49 enum glsl_base_type {
     50    /* Note: GLSL_TYPE_UINT, GLSL_TYPE_INT, and GLSL_TYPE_FLOAT must be 0, 1,
     51     * and 2 so that they will fit in the 2 bits of glsl_type::sampled_type.
     52     */
     53    GLSL_TYPE_UINT = 0,
     54    GLSL_TYPE_INT,
     55    GLSL_TYPE_FLOAT,
     56    GLSL_TYPE_DOUBLE,
     57    GLSL_TYPE_BOOL,
     58    GLSL_TYPE_SAMPLER,
     59    GLSL_TYPE_IMAGE,
     60    GLSL_TYPE_ATOMIC_UINT,
     61    GLSL_TYPE_STRUCT,
     62    GLSL_TYPE_INTERFACE,
     63    GLSL_TYPE_ARRAY,
     64    GLSL_TYPE_VOID,
     65    GLSL_TYPE_SUBROUTINE,
     66    GLSL_TYPE_FUNCTION,
     67    GLSL_TYPE_ERROR
     68 };
     69 
     70 static inline bool glsl_base_type_is_64bit(enum glsl_base_type type)
     71 {
     72    return type == GLSL_TYPE_DOUBLE;
     73 }
     74 
     75 enum glsl_sampler_dim {
     76    GLSL_SAMPLER_DIM_1D = 0,
     77    GLSL_SAMPLER_DIM_2D,
     78    GLSL_SAMPLER_DIM_3D,
     79    GLSL_SAMPLER_DIM_CUBE,
     80    GLSL_SAMPLER_DIM_RECT,
     81    GLSL_SAMPLER_DIM_BUF,
     82    GLSL_SAMPLER_DIM_EXTERNAL,
     83    GLSL_SAMPLER_DIM_MS,
     84    GLSL_SAMPLER_DIM_SUBPASS, /* for vulkan input attachments */
     85    GLSL_SAMPLER_DIM_SUBPASS_MS, /* for multisampled vulkan input attachments */
     86 };
     87 
     88 enum glsl_interface_packing {
     89    GLSL_INTERFACE_PACKING_STD140,
     90    GLSL_INTERFACE_PACKING_SHARED,
     91    GLSL_INTERFACE_PACKING_PACKED,
     92    GLSL_INTERFACE_PACKING_STD430
     93 };
     94 
     95 enum glsl_matrix_layout {
     96    /**
     97     * The layout of the matrix is inherited from the object containing the
     98     * matrix (the top level structure or the uniform block).
     99     */
    100    GLSL_MATRIX_LAYOUT_INHERITED,
    101 
    102    /**
    103     * Explicit column-major layout
    104     *
    105     * If a uniform block doesn't have an explicit layout set, it will default
    106     * to this layout.
    107     */
    108    GLSL_MATRIX_LAYOUT_COLUMN_MAJOR,
    109 
    110    /**
    111     * Row-major layout
    112     */
    113    GLSL_MATRIX_LAYOUT_ROW_MAJOR
    114 };
    115 
    116 enum {
    117    GLSL_PRECISION_NONE = 0,
    118    GLSL_PRECISION_HIGH,
    119    GLSL_PRECISION_MEDIUM,
    120    GLSL_PRECISION_LOW
    121 };
    122 
    123 #ifdef __cplusplus
    124 #include "GL/gl.h"
    125 #include "util/ralloc.h"
    126 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
    127 
    128 struct glsl_type {
    129    GLenum gl_type;
    130    glsl_base_type base_type;
    131 
    132    unsigned sampler_dimensionality:4; /**< \see glsl_sampler_dim */
    133    unsigned sampler_shadow:1;
    134    unsigned sampler_array:1;
    135    unsigned sampled_type:2;    /**< Type of data returned using this
    136 				* sampler or image.  Only \c
    137 				* GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
    138 				* and \c GLSL_TYPE_UINT are valid.
    139 				*/
    140    unsigned interface_packing:2;
    141    unsigned interface_row_major:1;
    142 
    143    /* Callers of this ralloc-based new need not call delete. It's
    144     * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
    145    static void* operator new(size_t size)
    146    {
    147       mtx_lock(&glsl_type::mutex);
    148 
    149       /* mem_ctx should have been created by the static members */
    150       assert(glsl_type::mem_ctx != NULL);
    151 
    152       void *type;
    153 
    154       type = ralloc_size(glsl_type::mem_ctx, size);
    155       assert(type != NULL);
    156 
    157       mtx_unlock(&glsl_type::mutex);
    158 
    159       return type;
    160    }
    161 
    162    /* If the user *does* call delete, that's OK, we will just
    163     * ralloc_free in that case. */
    164    static void operator delete(void *type)
    165    {
    166       mtx_lock(&glsl_type::mutex);
    167       ralloc_free(type);
    168       mtx_unlock(&glsl_type::mutex);
    169    }
    170 
    171    /**
    172     * \name Vector and matrix element counts
    173     *
    174     * For scalars, each of these values will be 1.  For non-numeric types
    175     * these will be 0.
    176     */
    177    /*@{*/
    178    uint8_t vector_elements;    /**< 1, 2, 3, or 4 vector elements. */
    179    uint8_t matrix_columns;     /**< 1, 2, 3, or 4 matrix columns. */
    180    /*@}*/
    181 
    182    /**
    183     * For \c GLSL_TYPE_ARRAY, this is the length of the array.  For
    184     * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
    185     * elements in the structure and the number of values pointed to by
    186     * \c fields.structure (below).
    187     */
    188    unsigned length;
    189 
    190    /**
    191     * Name of the data type
    192     *
    193     * Will never be \c NULL.
    194     */
    195    const char *name;
    196 
    197    /**
    198     * Subtype of composite data types.
    199     */
    200    union {
    201       const struct glsl_type *array;            /**< Type of array elements. */
    202       struct glsl_function_param *parameters;   /**< Parameters to function. */
    203       struct glsl_struct_field *structure;      /**< List of struct fields. */
    204    } fields;
    205 
    206    /**
    207     * \name Pointers to various public type singletons
    208     */
    209    /*@{*/
    210 #undef  DECL_TYPE
    211 #define DECL_TYPE(NAME, ...) \
    212    static const glsl_type *const NAME##_type;
    213 #undef  STRUCT_TYPE
    214 #define STRUCT_TYPE(NAME) \
    215    static const glsl_type *const struct_##NAME##_type;
    216 #include "compiler/builtin_type_macros.h"
    217    /*@}*/
    218 
    219    /**
    220     * Convenience accessors for vector types (shorter than get_instance()).
    221     * @{
    222     */
    223    static const glsl_type *vec(unsigned components);
    224    static const glsl_type *dvec(unsigned components);
    225    static const glsl_type *ivec(unsigned components);
    226    static const glsl_type *uvec(unsigned components);
    227    static const glsl_type *bvec(unsigned components);
    228    /**@}*/
    229 
    230    /**
    231     * For numeric and boolean derived types returns the basic scalar type
    232     *
    233     * If the type is a numeric or boolean scalar, vector, or matrix type,
    234     * this function gets the scalar type of the individual components.  For
    235     * all other types, including arrays of numeric or boolean types, the
    236     * error type is returned.
    237     */
    238    const glsl_type *get_base_type() const;
    239 
    240    /**
    241     * Get the basic scalar type which this type aggregates.
    242     *
    243     * If the type is a numeric or boolean scalar, vector, or matrix, or an
    244     * array of any of those, this function gets the scalar type of the
    245     * individual components.  For structs and arrays of structs, this function
    246     * returns the struct type.  For samplers and arrays of samplers, this
    247     * function returns the sampler type.
    248     */
    249    const glsl_type *get_scalar_type() const;
    250 
    251    /**
    252     * Get the instance of a built-in scalar, vector, or matrix type
    253     */
    254    static const glsl_type *get_instance(unsigned base_type, unsigned rows,
    255 					unsigned columns);
    256 
    257    /**
    258     * Get the instance of a sampler type
    259     */
    260    static const glsl_type *get_sampler_instance(enum glsl_sampler_dim dim,
    261                                                 bool shadow,
    262                                                 bool array,
    263                                                 glsl_base_type type);
    264 
    265    static const glsl_type *get_image_instance(enum glsl_sampler_dim dim,
    266                                               bool array, glsl_base_type type);
    267 
    268    /**
    269     * Get the instance of an array type
    270     */
    271    static const glsl_type *get_array_instance(const glsl_type *base,
    272 					      unsigned elements);
    273 
    274    /**
    275     * Get the instance of a record type
    276     */
    277    static const glsl_type *get_record_instance(const glsl_struct_field *fields,
    278 					       unsigned num_fields,
    279 					       const char *name);
    280 
    281    /**
    282     * Get the instance of an interface block type
    283     */
    284    static const glsl_type *get_interface_instance(const glsl_struct_field *fields,
    285 						  unsigned num_fields,
    286 						  enum glsl_interface_packing packing,
    287 						  bool row_major,
    288 						  const char *block_name);
    289 
    290    /**
    291     * Get the instance of an subroutine type
    292     */
    293    static const glsl_type *get_subroutine_instance(const char *subroutine_name);
    294 
    295    /**
    296     * Get the instance of a function type
    297     */
    298    static const glsl_type *get_function_instance(const struct glsl_type *return_type,
    299                                                  const glsl_function_param *parameters,
    300                                                  unsigned num_params);
    301 
    302    /**
    303     * Get the type resulting from a multiplication of \p type_a * \p type_b
    304     */
    305    static const glsl_type *get_mul_type(const glsl_type *type_a,
    306                                         const glsl_type *type_b);
    307 
    308    /**
    309     * Query the total number of scalars that make up a scalar, vector or matrix
    310     */
    311    unsigned components() const
    312    {
    313       return vector_elements * matrix_columns;
    314    }
    315 
    316    /**
    317     * Calculate the number of components slots required to hold this type
    318     *
    319     * This is used to determine how many uniform or varying locations a type
    320     * might occupy.
    321     */
    322    unsigned component_slots() const;
    323 
    324    /**
    325     * Calculate offset between the base location of the struct in
    326     * uniform storage and a struct member.
    327     * For the initial call, length is the index of the member to find the
    328     * offset for.
    329     */
    330    unsigned record_location_offset(unsigned length) const;
    331 
    332    /**
    333     * Calculate the number of unique values from glGetUniformLocation for the
    334     * elements of the type.
    335     *
    336     * This is used to allocate slots in the UniformRemapTable, the amount of
    337     * locations may not match with actual used storage space by the driver.
    338     */
    339    unsigned uniform_locations() const;
    340 
    341    /**
    342     * Used to count the number of varyings contained in the type ignoring
    343     * innermost array elements.
    344     */
    345    unsigned varying_count() const;
    346 
    347    /**
    348     * Calculate the number of attribute slots required to hold this type
    349     *
    350     * This implements the language rules of GLSL 1.50 for counting the number
    351     * of slots used by a vertex attribute.  It also determines the number of
    352     * varying slots the type will use up in the absence of varying packing
    353     * (and thus, it can be used to measure the number of varying slots used by
    354     * the varyings that are generated by lower_packed_varyings).
    355     *
    356     * For vertex shader attributes - doubles only take one slot.
    357     * For inter-shader varyings - dvec3/dvec4 take two slots.
    358     */
    359    unsigned count_attribute_slots(bool is_vertex_input) const;
    360 
    361    /**
    362     * Alignment in bytes of the start of this type in a std140 uniform
    363     * block.
    364     */
    365    unsigned std140_base_alignment(bool row_major) const;
    366 
    367    /** Size in bytes of this type in a std140 uniform block.
    368     *
    369     * Note that this is not GL_UNIFORM_SIZE (which is the number of
    370     * elements in the array)
    371     */
    372    unsigned std140_size(bool row_major) const;
    373 
    374    /**
    375     * Alignment in bytes of the start of this type in a std430 shader
    376     * storage block.
    377     */
    378    unsigned std430_base_alignment(bool row_major) const;
    379 
    380    /**
    381     * Calculate array stride in bytes of this type in a std430 shader storage
    382     * block.
    383     */
    384    unsigned std430_array_stride(bool row_major) const;
    385 
    386    /**
    387     * Size in bytes of this type in a std430 shader storage block.
    388     *
    389     * Note that this is not GL_BUFFER_SIZE
    390     */
    391    unsigned std430_size(bool row_major) const;
    392 
    393    /**
    394     * \brief Can this type be implicitly converted to another?
    395     *
    396     * \return True if the types are identical or if this type can be converted
    397     *         to \c desired according to Section 4.1.10 of the GLSL spec.
    398     *
    399     * \verbatim
    400     * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
    401     * Implicit Conversions:
    402     *
    403     *     In some situations, an expression and its type will be implicitly
    404     *     converted to a different type. The following table shows all allowed
    405     *     implicit conversions:
    406     *
    407     *     Type of expression | Can be implicitly converted to
    408     *     --------------------------------------------------
    409     *     int                  float
    410     *     uint
    411     *
    412     *     ivec2                vec2
    413     *     uvec2
    414     *
    415     *     ivec3                vec3
    416     *     uvec3
    417     *
    418     *     ivec4                vec4
    419     *     uvec4
    420     *
    421     *     There are no implicit array or structure conversions. For example,
    422     *     an array of int cannot be implicitly converted to an array of float.
    423     *     There are no implicit conversions between signed and unsigned
    424     *     integers.
    425     * \endverbatim
    426     */
    427    bool can_implicitly_convert_to(const glsl_type *desired,
    428                                   _mesa_glsl_parse_state *state) const;
    429 
    430    /**
    431     * Query whether or not a type is a scalar (non-vector and non-matrix).
    432     */
    433    bool is_scalar() const
    434    {
    435       return (vector_elements == 1)
    436 	 && (base_type >= GLSL_TYPE_UINT)
    437 	 && (base_type <= GLSL_TYPE_BOOL);
    438    }
    439 
    440    /**
    441     * Query whether or not a type is a vector
    442     */
    443    bool is_vector() const
    444    {
    445       return (vector_elements > 1)
    446 	 && (matrix_columns == 1)
    447 	 && (base_type >= GLSL_TYPE_UINT)
    448 	 && (base_type <= GLSL_TYPE_BOOL);
    449    }
    450 
    451    /**
    452     * Query whether or not a type is a matrix
    453     */
    454    bool is_matrix() const
    455    {
    456       /* GLSL only has float matrices. */
    457       return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT || base_type == GLSL_TYPE_DOUBLE);
    458    }
    459 
    460    /**
    461     * Query whether or not a type is a non-array numeric type
    462     */
    463    bool is_numeric() const
    464    {
    465       return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_DOUBLE);
    466    }
    467 
    468    /**
    469     * Query whether or not a type is an integral type
    470     */
    471    bool is_integer() const
    472    {
    473       return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
    474    }
    475 
    476    /**
    477     * Query whether or not type is an integral type, or for struct, interface
    478     * and array types, contains an integral type.
    479     */
    480    bool contains_integer() const;
    481 
    482    /**
    483     * Query whether or not type is a double type, or for struct, interface and
    484     * array types, contains a double type.
    485     */
    486    bool contains_double() const;
    487 
    488    /**
    489     * Query whether or not a type is a float type
    490     */
    491    bool is_float() const
    492    {
    493       return base_type == GLSL_TYPE_FLOAT;
    494    }
    495 
    496    /**
    497     * Query whether or not a type is a double type
    498     */
    499    bool is_double() const
    500    {
    501       return base_type == GLSL_TYPE_DOUBLE;
    502    }
    503 
    504    /**
    505     * Query whether a 64-bit type takes two slots.
    506     */
    507    bool is_dual_slot() const
    508    {
    509       return is_64bit() && vector_elements > 2;
    510    }
    511 
    512    /**
    513     * Query whether or not a type is 64-bit
    514     */
    515    bool is_64bit() const
    516    {
    517       return glsl_base_type_is_64bit(base_type);
    518    }
    519 
    520    /**
    521     * Query whether or not a type is a non-array boolean type
    522     */
    523    bool is_boolean() const
    524    {
    525       return base_type == GLSL_TYPE_BOOL;
    526    }
    527 
    528    /**
    529     * Query whether or not a type is a sampler
    530     */
    531    bool is_sampler() const
    532    {
    533       return base_type == GLSL_TYPE_SAMPLER;
    534    }
    535 
    536    /**
    537     * Query whether or not type is a sampler, or for struct, interface and
    538     * array types, contains a sampler.
    539     */
    540    bool contains_sampler() const;
    541 
    542    /**
    543     * Get the Mesa texture target index for a sampler type.
    544     */
    545    gl_texture_index sampler_index() const;
    546 
    547    /**
    548     * Query whether or not type is an image, or for struct, interface and
    549     * array types, contains an image.
    550     */
    551    bool contains_image() const;
    552 
    553    /**
    554     * Query whether or not a type is an image
    555     */
    556    bool is_image() const
    557    {
    558       return base_type == GLSL_TYPE_IMAGE;
    559    }
    560 
    561    /**
    562     * Query whether or not a type is an array
    563     */
    564    bool is_array() const
    565    {
    566       return base_type == GLSL_TYPE_ARRAY;
    567    }
    568 
    569    bool is_array_of_arrays() const
    570    {
    571       return is_array() && fields.array->is_array();
    572    }
    573 
    574    /**
    575     * Query whether or not a type is a record
    576     */
    577    bool is_record() const
    578    {
    579       return base_type == GLSL_TYPE_STRUCT;
    580    }
    581 
    582    /**
    583     * Query whether or not a type is an interface
    584     */
    585    bool is_interface() const
    586    {
    587       return base_type == GLSL_TYPE_INTERFACE;
    588    }
    589 
    590    /**
    591     * Query whether or not a type is the void type singleton.
    592     */
    593    bool is_void() const
    594    {
    595       return base_type == GLSL_TYPE_VOID;
    596    }
    597 
    598    /**
    599     * Query whether or not a type is the error type singleton.
    600     */
    601    bool is_error() const
    602    {
    603       return base_type == GLSL_TYPE_ERROR;
    604    }
    605 
    606    /**
    607     * Query if a type is unnamed/anonymous (named by the parser)
    608     */
    609 
    610    bool is_subroutine() const
    611    {
    612       return base_type == GLSL_TYPE_SUBROUTINE;
    613    }
    614    bool contains_subroutine() const;
    615 
    616    bool is_anonymous() const
    617    {
    618       return !strncmp(name, "#anon", 5);
    619    }
    620 
    621    /**
    622     * Get the type stripped of any arrays
    623     *
    624     * \return
    625     * Pointer to the type of elements of the first non-array type for array
    626     * types, or pointer to itself for non-array types.
    627     */
    628    const glsl_type *without_array() const
    629    {
    630       const glsl_type *t = this;
    631 
    632       while (t->is_array())
    633          t = t->fields.array;
    634 
    635       return t;
    636    }
    637 
    638    /**
    639     * Return the total number of elements in an array including the elements
    640     * in arrays of arrays.
    641     */
    642    unsigned arrays_of_arrays_size() const
    643    {
    644       if (!is_array())
    645          return 0;
    646 
    647       unsigned size = length;
    648       const glsl_type *base_type = fields.array;
    649 
    650       while (base_type->is_array()) {
    651          size = size * base_type->length;
    652          base_type = base_type->fields.array;
    653       }
    654       return size;
    655    }
    656 
    657    /**
    658     * Return the amount of atomic counter storage required for a type.
    659     */
    660    unsigned atomic_size() const
    661    {
    662       if (base_type == GLSL_TYPE_ATOMIC_UINT)
    663          return ATOMIC_COUNTER_SIZE;
    664       else if (is_array())
    665          return length * fields.array->atomic_size();
    666       else
    667          return 0;
    668    }
    669 
    670    /**
    671     * Return whether a type contains any atomic counters.
    672     */
    673    bool contains_atomic() const
    674    {
    675       return atomic_size() > 0;
    676    }
    677 
    678    /**
    679     * Return whether a type contains any opaque types.
    680     */
    681    bool contains_opaque() const;
    682 
    683    /**
    684     * Query the full type of a matrix row
    685     *
    686     * \return
    687     * If the type is not a matrix, \c glsl_type::error_type is returned.
    688     * Otherwise a type matching the rows of the matrix is returned.
    689     */
    690    const glsl_type *row_type() const
    691    {
    692       return is_matrix()
    693 	 ? get_instance(base_type, matrix_columns, 1)
    694 	 : error_type;
    695    }
    696 
    697    /**
    698     * Query the full type of a matrix column
    699     *
    700     * \return
    701     * If the type is not a matrix, \c glsl_type::error_type is returned.
    702     * Otherwise a type matching the columns of the matrix is returned.
    703     */
    704    const glsl_type *column_type() const
    705    {
    706       return is_matrix()
    707 	 ? get_instance(base_type, vector_elements, 1)
    708 	 : error_type;
    709    }
    710 
    711    /**
    712     * Get the type of a structure field
    713     *
    714     * \return
    715     * Pointer to the type of the named field.  If the type is not a structure
    716     * or the named field does not exist, \c glsl_type::error_type is returned.
    717     */
    718    const glsl_type *field_type(const char *name) const;
    719 
    720    /**
    721     * Get the location of a field within a record type
    722     */
    723    int field_index(const char *name) const;
    724 
    725    /**
    726     * Query the number of elements in an array type
    727     *
    728     * \return
    729     * The number of elements in the array for array types or -1 for non-array
    730     * types.  If the number of elements in the array has not yet been declared,
    731     * zero is returned.
    732     */
    733    int array_size() const
    734    {
    735       return is_array() ? length : -1;
    736    }
    737 
    738    /**
    739     * Query whether the array size for all dimensions has been declared.
    740     */
    741    bool is_unsized_array() const
    742    {
    743       return is_array() && length == 0;
    744    }
    745 
    746    /**
    747     * Return the number of coordinate components needed for this
    748     * sampler or image type.
    749     *
    750     * This is based purely on the sampler's dimensionality.  For example, this
    751     * returns 1 for sampler1D, and 3 for sampler2DArray.
    752     *
    753     * Note that this is often different than actual coordinate type used in
    754     * a texturing built-in function, since those pack additional values (such
    755     * as the shadow comparator or projector) into the coordinate type.
    756     */
    757    int coordinate_components() const;
    758 
    759    /**
    760     * Compare a record type against another record type.
    761     *
    762     * This is useful for matching record types declared across shader stages.
    763     * The option to not match locations is to deal with places where the
    764     * same struct is defined in a block which has a location set on it.
    765     */
    766    bool record_compare(const glsl_type *b, bool match_locations = true) const;
    767 
    768    /**
    769     * Get the type interface packing.
    770     */
    771    enum glsl_interface_packing get_interface_packing() const
    772    {
    773       return (enum glsl_interface_packing)interface_packing;
    774    }
    775 
    776    /**
    777     * Check if the type interface is row major
    778     */
    779    bool get_interface_row_major() const
    780    {
    781       return (bool) interface_row_major;
    782    }
    783 
    784 private:
    785 
    786    static mtx_t mutex;
    787 
    788    /**
    789     * ralloc context for all glsl_type allocations
    790     *
    791     * Set on the first call to \c glsl_type::new.
    792     */
    793    static void *mem_ctx;
    794 
    795    void init_ralloc_type_ctx(void);
    796 
    797    /** Constructor for vector and matrix types */
    798    glsl_type(GLenum gl_type,
    799 	     glsl_base_type base_type, unsigned vector_elements,
    800 	     unsigned matrix_columns, const char *name);
    801 
    802    /** Constructor for sampler or image types */
    803    glsl_type(GLenum gl_type, glsl_base_type base_type,
    804 	     enum glsl_sampler_dim dim, bool shadow, bool array,
    805 	     unsigned type, const char *name);
    806 
    807    /** Constructor for record types */
    808    glsl_type(const glsl_struct_field *fields, unsigned num_fields,
    809 	     const char *name);
    810 
    811    /** Constructor for interface types */
    812    glsl_type(const glsl_struct_field *fields, unsigned num_fields,
    813 	     enum glsl_interface_packing packing,
    814 	     bool row_major, const char *name);
    815 
    816    /** Constructor for interface types */
    817    glsl_type(const glsl_type *return_type,
    818              const glsl_function_param *params, unsigned num_params);
    819 
    820    /** Constructor for array types */
    821    glsl_type(const glsl_type *array, unsigned length);
    822 
    823    /** Constructor for subroutine types */
    824    glsl_type(const char *name);
    825 
    826    /** Hash table containing the known array types. */
    827    static struct hash_table *array_types;
    828 
    829    /** Hash table containing the known record types. */
    830    static struct hash_table *record_types;
    831 
    832    /** Hash table containing the known interface types. */
    833    static struct hash_table *interface_types;
    834 
    835    /** Hash table containing the known subroutine types. */
    836    static struct hash_table *subroutine_types;
    837 
    838    /** Hash table containing the known function types. */
    839    static struct hash_table *function_types;
    840 
    841    static bool record_key_compare(const void *a, const void *b);
    842    static unsigned record_key_hash(const void *key);
    843 
    844    /**
    845     * \name Built-in type flyweights
    846     */
    847    /*@{*/
    848 #undef  DECL_TYPE
    849 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
    850 #undef  STRUCT_TYPE
    851 #define STRUCT_TYPE(NAME)        static const glsl_type _struct_##NAME##_type;
    852 #include "compiler/builtin_type_macros.h"
    853    /*@}*/
    854 
    855    /**
    856     * \name Friend functions.
    857     *
    858     * These functions are friends because they must have C linkage and the
    859     * need to call various private methods or access various private static
    860     * data.
    861     */
    862    /*@{*/
    863    friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
    864    friend void _mesa_glsl_release_types(void);
    865    /*@}*/
    866 };
    867 
    868 #undef DECL_TYPE
    869 #undef STRUCT_TYPE
    870 #endif /* __cplusplus */
    871 
    872 struct glsl_struct_field {
    873    const struct glsl_type *type;
    874    const char *name;
    875 
    876    /**
    877     * For interface blocks, gl_varying_slot corresponding to the input/output
    878     * if this is a built-in input/output (i.e. a member of the built-in
    879     * gl_PerVertex interface block); -1 otherwise.
    880     *
    881     * Ignored for structs.
    882     */
    883    int location;
    884 
    885    /**
    886     * For interface blocks, members may have an explicit byte offset
    887     * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
    888     *
    889     * Unless used for xfb_offset this field is ignored for structs.
    890     */
    891    int offset;
    892 
    893    /**
    894     * For interface blocks, members may define a transform feedback buffer;
    895     * -1 otherwise.
    896     */
    897    int xfb_buffer;
    898 
    899    /**
    900     * For interface blocks, members may define a transform feedback stride;
    901     * -1 otherwise.
    902     */
    903    int xfb_stride;
    904 
    905    /**
    906     * For interface blocks, the interpolation mode (as in
    907     * ir_variable::interpolation).  0 otherwise.
    908     */
    909    unsigned interpolation:2;
    910 
    911    /**
    912     * For interface blocks, 1 if this variable uses centroid interpolation (as
    913     * in ir_variable::centroid).  0 otherwise.
    914     */
    915    unsigned centroid:1;
    916 
    917    /**
    918     * For interface blocks, 1 if this variable uses sample interpolation (as
    919     * in ir_variable::sample). 0 otherwise.
    920     */
    921    unsigned sample:1;
    922 
    923    /**
    924     * Layout of the matrix.  Uses glsl_matrix_layout values.
    925     */
    926    unsigned matrix_layout:2;
    927 
    928    /**
    929     * For interface blocks, 1 if this variable is a per-patch input or output
    930     * (as in ir_variable::patch). 0 otherwise.
    931     */
    932    unsigned patch:1;
    933 
    934    /**
    935     * Precision qualifier
    936     */
    937    unsigned precision:2;
    938 
    939    /**
    940     * Image qualifiers, applicable to buffer variables defined in shader
    941     * storage buffer objects (SSBOs)
    942     */
    943    unsigned image_read_only:1;
    944    unsigned image_write_only:1;
    945    unsigned image_coherent:1;
    946    unsigned image_volatile:1;
    947    unsigned image_restrict:1;
    948 
    949    /**
    950     * Any of the xfb_* qualifiers trigger the shader to be in transform
    951     * feedback mode so we need to keep track of whether the buffer was
    952     * explicitly set or if its just been assigned the default global value.
    953     */
    954    unsigned explicit_xfb_buffer:1;
    955 
    956    unsigned implicit_sized_array:1;
    957 #ifdef __cplusplus
    958    glsl_struct_field(const struct glsl_type *_type, const char *_name)
    959       : type(_type), name(_name), location(-1), offset(0), xfb_buffer(0),
    960         xfb_stride(0), interpolation(0), centroid(0),
    961         sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED), patch(0),
    962         precision(GLSL_PRECISION_NONE), image_read_only(0), image_write_only(0),
    963         image_coherent(0), image_volatile(0), image_restrict(0),
    964         explicit_xfb_buffer(0), implicit_sized_array(0)
    965    {
    966       /* empty */
    967    }
    968 
    969    glsl_struct_field()
    970    {
    971       /* empty */
    972    }
    973 #endif
    974 };
    975 
    976 struct glsl_function_param {
    977    const struct glsl_type *type;
    978 
    979    bool in;
    980    bool out;
    981 };
    982 
    983 static inline unsigned int
    984 glsl_align(unsigned int a, unsigned int align)
    985 {
    986    return (a + align - 1) / align * align;
    987 }
    988 
    989 #endif /* GLSL_TYPES_H */
    990