Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

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