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