11591693c7b415e9869157c711fe11263c95d74eDavid Li/* -*- c++ -*- */
21591693c7b415e9869157c711fe11263c95d74eDavid Li/*
31591693c7b415e9869157c711fe11263c95d74eDavid Li * Copyright © 2009 Intel Corporation
41591693c7b415e9869157c711fe11263c95d74eDavid Li *
51591693c7b415e9869157c711fe11263c95d74eDavid Li * Permission is hereby granted, free of charge, to any person obtaining a
61591693c7b415e9869157c711fe11263c95d74eDavid Li * copy of this software and associated documentation files (the "Software"),
71591693c7b415e9869157c711fe11263c95d74eDavid Li * to deal in the Software without restriction, including without limitation
81591693c7b415e9869157c711fe11263c95d74eDavid Li * the rights to use, copy, modify, merge, publish, distribute, sublicense,
91591693c7b415e9869157c711fe11263c95d74eDavid Li * and/or sell copies of the Software, and to permit persons to whom the
101591693c7b415e9869157c711fe11263c95d74eDavid Li * Software is furnished to do so, subject to the following conditions:
111591693c7b415e9869157c711fe11263c95d74eDavid Li *
121591693c7b415e9869157c711fe11263c95d74eDavid Li * The above copyright notice and this permission notice (including the next
131591693c7b415e9869157c711fe11263c95d74eDavid Li * paragraph) shall be included in all copies or substantial portions of the
141591693c7b415e9869157c711fe11263c95d74eDavid Li * Software.
151591693c7b415e9869157c711fe11263c95d74eDavid Li *
161591693c7b415e9869157c711fe11263c95d74eDavid Li * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171591693c7b415e9869157c711fe11263c95d74eDavid Li * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181591693c7b415e9869157c711fe11263c95d74eDavid Li * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
191591693c7b415e9869157c711fe11263c95d74eDavid Li * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201591693c7b415e9869157c711fe11263c95d74eDavid Li * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
211591693c7b415e9869157c711fe11263c95d74eDavid Li * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
221591693c7b415e9869157c711fe11263c95d74eDavid Li * DEALINGS IN THE SOFTWARE.
231591693c7b415e9869157c711fe11263c95d74eDavid Li */
241591693c7b415e9869157c711fe11263c95d74eDavid Li
251591693c7b415e9869157c711fe11263c95d74eDavid Li#pragma once
261591693c7b415e9869157c711fe11263c95d74eDavid Li#ifndef GLSL_TYPES_H
271591693c7b415e9869157c711fe11263c95d74eDavid Li#define GLSL_TYPES_H
281591693c7b415e9869157c711fe11263c95d74eDavid Li
291591693c7b415e9869157c711fe11263c95d74eDavid Li#include <cstring>
301591693c7b415e9869157c711fe11263c95d74eDavid Li#include <cassert>
311591693c7b415e9869157c711fe11263c95d74eDavid Li
321591693c7b415e9869157c711fe11263c95d74eDavid Liextern "C" {
3313fea0fc797fa0d4c236db5aa2e6a23fc0e450dbDavid Li#include "GLES2/gl2.h"
34d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li#include <hieralloc.h>
351591693c7b415e9869157c711fe11263c95d74eDavid Li}
361591693c7b415e9869157c711fe11263c95d74eDavid Li
371591693c7b415e9869157c711fe11263c95d74eDavid Listruct _mesa_glsl_parse_state;
381591693c7b415e9869157c711fe11263c95d74eDavid Listruct glsl_symbol_table;
391591693c7b415e9869157c711fe11263c95d74eDavid Li
401591693c7b415e9869157c711fe11263c95d74eDavid Liextern "C" void
411591693c7b415e9869157c711fe11263c95d74eDavid Li_mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
421591693c7b415e9869157c711fe11263c95d74eDavid Li
431591693c7b415e9869157c711fe11263c95d74eDavid Liextern "C" void
441591693c7b415e9869157c711fe11263c95d74eDavid Li_mesa_glsl_release_types(void);
451591693c7b415e9869157c711fe11263c95d74eDavid Li
461591693c7b415e9869157c711fe11263c95d74eDavid Lienum glsl_base_type {
471591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_TYPE_UINT = 0,
481591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_TYPE_INT,
491591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_TYPE_FLOAT,
501591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_TYPE_BOOL,
511591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_TYPE_SAMPLER,
521591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_TYPE_STRUCT,
531591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_TYPE_ARRAY,
541591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_TYPE_VOID,
551591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_TYPE_ERROR
561591693c7b415e9869157c711fe11263c95d74eDavid Li};
571591693c7b415e9869157c711fe11263c95d74eDavid Li
581591693c7b415e9869157c711fe11263c95d74eDavid Lienum glsl_sampler_dim {
591591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_SAMPLER_DIM_1D = 0,
601591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_SAMPLER_DIM_2D,
611591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_SAMPLER_DIM_3D,
621591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_SAMPLER_DIM_CUBE,
631591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_SAMPLER_DIM_RECT,
641591693c7b415e9869157c711fe11263c95d74eDavid Li   GLSL_SAMPLER_DIM_BUF
651591693c7b415e9869157c711fe11263c95d74eDavid Li};
661591693c7b415e9869157c711fe11263c95d74eDavid Li
671591693c7b415e9869157c711fe11263c95d74eDavid Li
681591693c7b415e9869157c711fe11263c95d74eDavid Listruct glsl_type {
691591693c7b415e9869157c711fe11263c95d74eDavid Li   GLenum gl_type;
701591693c7b415e9869157c711fe11263c95d74eDavid Li   glsl_base_type base_type;
711591693c7b415e9869157c711fe11263c95d74eDavid Li
721591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned sampler_dimensionality:3;
731591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned sampler_shadow:1;
741591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned sampler_array:1;
751591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned sampler_type:2;    /**< Type of data returned using this sampler.
761591693c7b415e9869157c711fe11263c95d74eDavid Li				* only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
771591693c7b415e9869157c711fe11263c95d74eDavid Li				* and \c GLSL_TYPE_UINT are valid.
781591693c7b415e9869157c711fe11263c95d74eDavid Li				*/
791591693c7b415e9869157c711fe11263c95d74eDavid Li
80d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li   /* Callers of this hieralloc-based new need not call delete. It's
81d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li    * easier to just hieralloc_free 'mem_ctx' (or any of its ancestors). */
821591693c7b415e9869157c711fe11263c95d74eDavid Li   static void* operator new(size_t size)
831591693c7b415e9869157c711fe11263c95d74eDavid Li   {
841591693c7b415e9869157c711fe11263c95d74eDavid Li      if (glsl_type::mem_ctx == NULL) {
85d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li	 glsl_type::mem_ctx = hieralloc_init("glsl_type");
861591693c7b415e9869157c711fe11263c95d74eDavid Li	 assert(glsl_type::mem_ctx != NULL);
871591693c7b415e9869157c711fe11263c95d74eDavid Li      }
881591693c7b415e9869157c711fe11263c95d74eDavid Li
891591693c7b415e9869157c711fe11263c95d74eDavid Li      void *type;
901591693c7b415e9869157c711fe11263c95d74eDavid Li
91d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li      type = hieralloc_size(glsl_type::mem_ctx, size);
921591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(type != NULL);
931591693c7b415e9869157c711fe11263c95d74eDavid Li
941591693c7b415e9869157c711fe11263c95d74eDavid Li      return type;
951591693c7b415e9869157c711fe11263c95d74eDavid Li   }
961591693c7b415e9869157c711fe11263c95d74eDavid Li
971591693c7b415e9869157c711fe11263c95d74eDavid Li   /* If the user *does* call delete, that's OK, we will just
98d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li    * hieralloc_free in that case. */
991591693c7b415e9869157c711fe11263c95d74eDavid Li   static void operator delete(void *type)
1001591693c7b415e9869157c711fe11263c95d74eDavid Li   {
101d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li      hieralloc_free(type);
1021591693c7b415e9869157c711fe11263c95d74eDavid Li   }
1031591693c7b415e9869157c711fe11263c95d74eDavid Li
1041591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
1051591693c7b415e9869157c711fe11263c95d74eDavid Li    * \name Vector and matrix element counts
1061591693c7b415e9869157c711fe11263c95d74eDavid Li    *
1071591693c7b415e9869157c711fe11263c95d74eDavid Li    * For scalars, each of these values will be 1.  For non-numeric types
1081591693c7b415e9869157c711fe11263c95d74eDavid Li    * these will be 0.
1091591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1101591693c7b415e9869157c711fe11263c95d74eDavid Li   /*@{*/
1111591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned vector_elements:3; /**< 1, 2, 3, or 4 vector elements. */
1121591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned matrix_columns:3;  /**< 1, 2, 3, or 4 matrix columns. */
1131591693c7b415e9869157c711fe11263c95d74eDavid Li   /*@}*/
1141591693c7b415e9869157c711fe11263c95d74eDavid Li
1151591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
1161591693c7b415e9869157c711fe11263c95d74eDavid Li    * Name of the data type
1171591693c7b415e9869157c711fe11263c95d74eDavid Li    *
1181591693c7b415e9869157c711fe11263c95d74eDavid Li    * This may be \c NULL for anonymous structures, for arrays, or for
1191591693c7b415e9869157c711fe11263c95d74eDavid Li    * function types.
1201591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1211591693c7b415e9869157c711fe11263c95d74eDavid Li   const char *name;
1221591693c7b415e9869157c711fe11263c95d74eDavid Li
1231591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
1241591693c7b415e9869157c711fe11263c95d74eDavid Li    * For \c GLSL_TYPE_ARRAY, this is the length of the array.  For
1251591693c7b415e9869157c711fe11263c95d74eDavid Li    * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
1261591693c7b415e9869157c711fe11263c95d74eDavid Li    * the number of values pointed to by \c fields.structure (below).
1271591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1281591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned length;
1291591693c7b415e9869157c711fe11263c95d74eDavid Li
1301591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
1311591693c7b415e9869157c711fe11263c95d74eDavid Li    * Subtype of composite data types.
1321591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1331591693c7b415e9869157c711fe11263c95d74eDavid Li   union {
1341591693c7b415e9869157c711fe11263c95d74eDavid Li      const struct glsl_type *array;            /**< Type of array elements. */
1351591693c7b415e9869157c711fe11263c95d74eDavid Li      const struct glsl_type *parameters;       /**< Parameters to function. */
1361591693c7b415e9869157c711fe11263c95d74eDavid Li      struct glsl_struct_field *structure;      /**< List of struct fields. */
1371591693c7b415e9869157c711fe11263c95d74eDavid Li   } fields;
1381591693c7b415e9869157c711fe11263c95d74eDavid Li
1391591693c7b415e9869157c711fe11263c95d74eDavid Li
1401591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
1411591693c7b415e9869157c711fe11263c95d74eDavid Li    * \name Pointers to various public type singletons
1421591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1431591693c7b415e9869157c711fe11263c95d74eDavid Li   /*@{*/
1441591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const error_type;
1451591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const int_type;
1461591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const ivec4_type;
1471591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const uint_type;
1481591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const uvec2_type;
1491591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const uvec3_type;
1501591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const uvec4_type;
1511591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const float_type;
1521591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const vec2_type;
1531591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const vec3_type;
1541591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const vec4_type;
1551591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const bool_type;
1561591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const mat2_type;
1571591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const mat2x3_type;
1581591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const mat2x4_type;
1591591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const mat3x2_type;
1601591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const mat3_type;
1611591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const mat3x4_type;
1621591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const mat4x2_type;
1631591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const mat4x3_type;
1641591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *const mat4_type;
1651591693c7b415e9869157c711fe11263c95d74eDavid Li   /*@}*/
1661591693c7b415e9869157c711fe11263c95d74eDavid Li
1671591693c7b415e9869157c711fe11263c95d74eDavid Li
1681591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
1691591693c7b415e9869157c711fe11263c95d74eDavid Li    * For numeric and boolean derrived types returns the basic scalar type
1701591693c7b415e9869157c711fe11263c95d74eDavid Li    *
1711591693c7b415e9869157c711fe11263c95d74eDavid Li    * If the type is a numeric or boolean scalar, vector, or matrix type,
1721591693c7b415e9869157c711fe11263c95d74eDavid Li    * this function gets the scalar type of the individual components.  For
1731591693c7b415e9869157c711fe11263c95d74eDavid Li    * all other types, including arrays of numeric or boolean types, the
1741591693c7b415e9869157c711fe11263c95d74eDavid Li    * error type is returned.
1751591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1761591693c7b415e9869157c711fe11263c95d74eDavid Li   const glsl_type *get_base_type() const;
1771591693c7b415e9869157c711fe11263c95d74eDavid Li
1781591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
1791591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query the type of elements in an array
1801591693c7b415e9869157c711fe11263c95d74eDavid Li    *
1811591693c7b415e9869157c711fe11263c95d74eDavid Li    * \return
1821591693c7b415e9869157c711fe11263c95d74eDavid Li    * Pointer to the type of elements in the array for array types, or \c NULL
1831591693c7b415e9869157c711fe11263c95d74eDavid Li    * for non-array types.
1841591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1851591693c7b415e9869157c711fe11263c95d74eDavid Li   const glsl_type *element_type() const
1861591693c7b415e9869157c711fe11263c95d74eDavid Li   {
1871591693c7b415e9869157c711fe11263c95d74eDavid Li      return is_array() ? fields.array : NULL;
1881591693c7b415e9869157c711fe11263c95d74eDavid Li   }
1891591693c7b415e9869157c711fe11263c95d74eDavid Li
1901591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
1911591693c7b415e9869157c711fe11263c95d74eDavid Li    * Get the instance of a built-in scalar, vector, or matrix type
1921591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1931591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *get_instance(unsigned base_type, unsigned rows,
1941591693c7b415e9869157c711fe11263c95d74eDavid Li					unsigned columns);
1951591693c7b415e9869157c711fe11263c95d74eDavid Li
1961591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
1971591693c7b415e9869157c711fe11263c95d74eDavid Li    * Get the instance of an array type
1981591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1991591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *get_array_instance(const glsl_type *base,
2001591693c7b415e9869157c711fe11263c95d74eDavid Li					      unsigned elements);
2011591693c7b415e9869157c711fe11263c95d74eDavid Li
2021591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
2031591693c7b415e9869157c711fe11263c95d74eDavid Li    * Get the instance of a record type
2041591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2051591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type *get_record_instance(const glsl_struct_field *fields,
2061591693c7b415e9869157c711fe11263c95d74eDavid Li					       unsigned num_fields,
2071591693c7b415e9869157c711fe11263c95d74eDavid Li					       const char *name);
2081591693c7b415e9869157c711fe11263c95d74eDavid Li
2091591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
2101591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query the total number of scalars that make up a scalar, vector or matrix
2111591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2121591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned components() const
2131591693c7b415e9869157c711fe11263c95d74eDavid Li   {
2141591693c7b415e9869157c711fe11263c95d74eDavid Li      return vector_elements * matrix_columns;
2151591693c7b415e9869157c711fe11263c95d74eDavid Li   }
2161591693c7b415e9869157c711fe11263c95d74eDavid Li
2171591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
2181591693c7b415e9869157c711fe11263c95d74eDavid Li    * Calculate the number of components slots required to hold this type
2191591693c7b415e9869157c711fe11263c95d74eDavid Li    *
2201591693c7b415e9869157c711fe11263c95d74eDavid Li    * This is used to determine how many uniform or varying locations a type
2211591693c7b415e9869157c711fe11263c95d74eDavid Li    * might occupy.
2221591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2231591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned component_slots() const;
2241591693c7b415e9869157c711fe11263c95d74eDavid Li
2251591693c7b415e9869157c711fe11263c95d74eDavid Li
2261591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
2271591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query whether or not a type is a scalar (non-vector and non-matrix).
2281591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2291591693c7b415e9869157c711fe11263c95d74eDavid Li   bool is_scalar() const
2301591693c7b415e9869157c711fe11263c95d74eDavid Li   {
2311591693c7b415e9869157c711fe11263c95d74eDavid Li      return (vector_elements == 1)
2321591693c7b415e9869157c711fe11263c95d74eDavid Li	 && (base_type >= GLSL_TYPE_UINT)
2331591693c7b415e9869157c711fe11263c95d74eDavid Li	 && (base_type <= GLSL_TYPE_BOOL);
2341591693c7b415e9869157c711fe11263c95d74eDavid Li   }
2351591693c7b415e9869157c711fe11263c95d74eDavid Li
2361591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
2371591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query whether or not a type is a vector
2381591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2391591693c7b415e9869157c711fe11263c95d74eDavid Li   bool is_vector() const
2401591693c7b415e9869157c711fe11263c95d74eDavid Li   {
2411591693c7b415e9869157c711fe11263c95d74eDavid Li      return (vector_elements > 1)
2421591693c7b415e9869157c711fe11263c95d74eDavid Li	 && (matrix_columns == 1)
2431591693c7b415e9869157c711fe11263c95d74eDavid Li	 && (base_type >= GLSL_TYPE_UINT)
2441591693c7b415e9869157c711fe11263c95d74eDavid Li	 && (base_type <= GLSL_TYPE_BOOL);
2451591693c7b415e9869157c711fe11263c95d74eDavid Li   }
2461591693c7b415e9869157c711fe11263c95d74eDavid Li
2471591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
2481591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query whether or not a type is a matrix
2491591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2501591693c7b415e9869157c711fe11263c95d74eDavid Li   bool is_matrix() const
2511591693c7b415e9869157c711fe11263c95d74eDavid Li   {
2521591693c7b415e9869157c711fe11263c95d74eDavid Li      /* GLSL only has float matrices. */
2531591693c7b415e9869157c711fe11263c95d74eDavid Li      return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT);
2541591693c7b415e9869157c711fe11263c95d74eDavid Li   }
2551591693c7b415e9869157c711fe11263c95d74eDavid Li
2561591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
2571591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query whether or not a type is a non-array numeric type
2581591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2591591693c7b415e9869157c711fe11263c95d74eDavid Li   bool is_numeric() const
2601591693c7b415e9869157c711fe11263c95d74eDavid Li   {
2611591693c7b415e9869157c711fe11263c95d74eDavid Li      return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT);
2621591693c7b415e9869157c711fe11263c95d74eDavid Li   }
2631591693c7b415e9869157c711fe11263c95d74eDavid Li
2641591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
2651591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query whether or not a type is an integral type
2661591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2671591693c7b415e9869157c711fe11263c95d74eDavid Li   bool is_integer() const
2681591693c7b415e9869157c711fe11263c95d74eDavid Li   {
2691591693c7b415e9869157c711fe11263c95d74eDavid Li      return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
2701591693c7b415e9869157c711fe11263c95d74eDavid Li   }
2711591693c7b415e9869157c711fe11263c95d74eDavid Li
2721591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
2731591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query whether or not a type is a float type
2741591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2751591693c7b415e9869157c711fe11263c95d74eDavid Li   bool is_float() const
2761591693c7b415e9869157c711fe11263c95d74eDavid Li   {
2771591693c7b415e9869157c711fe11263c95d74eDavid Li      return base_type == GLSL_TYPE_FLOAT;
2781591693c7b415e9869157c711fe11263c95d74eDavid Li   }
2791591693c7b415e9869157c711fe11263c95d74eDavid Li
2801591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
2811591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query whether or not a type is a non-array boolean type
2821591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2831591693c7b415e9869157c711fe11263c95d74eDavid Li   bool is_boolean() const
2841591693c7b415e9869157c711fe11263c95d74eDavid Li   {
2851591693c7b415e9869157c711fe11263c95d74eDavid Li      return base_type == GLSL_TYPE_BOOL;
2861591693c7b415e9869157c711fe11263c95d74eDavid Li   }
2871591693c7b415e9869157c711fe11263c95d74eDavid Li
2881591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
2891591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query whether or not a type is a sampler
2901591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2911591693c7b415e9869157c711fe11263c95d74eDavid Li   bool is_sampler() const
2921591693c7b415e9869157c711fe11263c95d74eDavid Li   {
2931591693c7b415e9869157c711fe11263c95d74eDavid Li      return base_type == GLSL_TYPE_SAMPLER;
2941591693c7b415e9869157c711fe11263c95d74eDavid Li   }
2951591693c7b415e9869157c711fe11263c95d74eDavid Li
2961591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
2971591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query whether or not a type is an array
2981591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2991591693c7b415e9869157c711fe11263c95d74eDavid Li   bool is_array() const
3001591693c7b415e9869157c711fe11263c95d74eDavid Li   {
3011591693c7b415e9869157c711fe11263c95d74eDavid Li      return base_type == GLSL_TYPE_ARRAY;
3021591693c7b415e9869157c711fe11263c95d74eDavid Li   }
3031591693c7b415e9869157c711fe11263c95d74eDavid Li
3041591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
3051591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query whether or not a type is a record
3061591693c7b415e9869157c711fe11263c95d74eDavid Li    */
3071591693c7b415e9869157c711fe11263c95d74eDavid Li   bool is_record() const
3081591693c7b415e9869157c711fe11263c95d74eDavid Li   {
3091591693c7b415e9869157c711fe11263c95d74eDavid Li      return base_type == GLSL_TYPE_STRUCT;
3101591693c7b415e9869157c711fe11263c95d74eDavid Li   }
3111591693c7b415e9869157c711fe11263c95d74eDavid Li
3121591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
3131591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query whether or not a type is the void type singleton.
3141591693c7b415e9869157c711fe11263c95d74eDavid Li    */
3151591693c7b415e9869157c711fe11263c95d74eDavid Li   bool is_void() const
3161591693c7b415e9869157c711fe11263c95d74eDavid Li   {
3171591693c7b415e9869157c711fe11263c95d74eDavid Li      return base_type == GLSL_TYPE_VOID;
3181591693c7b415e9869157c711fe11263c95d74eDavid Li   }
3191591693c7b415e9869157c711fe11263c95d74eDavid Li
3201591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
3211591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query whether or not a type is the error type singleton.
3221591693c7b415e9869157c711fe11263c95d74eDavid Li    */
3231591693c7b415e9869157c711fe11263c95d74eDavid Li   bool is_error() const
3241591693c7b415e9869157c711fe11263c95d74eDavid Li   {
3251591693c7b415e9869157c711fe11263c95d74eDavid Li      return base_type == GLSL_TYPE_ERROR;
3261591693c7b415e9869157c711fe11263c95d74eDavid Li   }
3271591693c7b415e9869157c711fe11263c95d74eDavid Li
3281591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
3291591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query the full type of a matrix row
3301591693c7b415e9869157c711fe11263c95d74eDavid Li    *
3311591693c7b415e9869157c711fe11263c95d74eDavid Li    * \return
3321591693c7b415e9869157c711fe11263c95d74eDavid Li    * If the type is not a matrix, \c glsl_type::error_type is returned.
3331591693c7b415e9869157c711fe11263c95d74eDavid Li    * Otherwise a type matching the rows of the matrix is returned.
3341591693c7b415e9869157c711fe11263c95d74eDavid Li    */
3351591693c7b415e9869157c711fe11263c95d74eDavid Li   const glsl_type *row_type() const
3361591693c7b415e9869157c711fe11263c95d74eDavid Li   {
3371591693c7b415e9869157c711fe11263c95d74eDavid Li      return is_matrix()
3381591693c7b415e9869157c711fe11263c95d74eDavid Li	 ? get_instance(base_type, matrix_columns, 1)
3391591693c7b415e9869157c711fe11263c95d74eDavid Li	 : error_type;
3401591693c7b415e9869157c711fe11263c95d74eDavid Li   }
3411591693c7b415e9869157c711fe11263c95d74eDavid Li
3421591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
3431591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query the full type of a matrix column
3441591693c7b415e9869157c711fe11263c95d74eDavid Li    *
3451591693c7b415e9869157c711fe11263c95d74eDavid Li    * \return
3461591693c7b415e9869157c711fe11263c95d74eDavid Li    * If the type is not a matrix, \c glsl_type::error_type is returned.
3471591693c7b415e9869157c711fe11263c95d74eDavid Li    * Otherwise a type matching the columns of the matrix is returned.
3481591693c7b415e9869157c711fe11263c95d74eDavid Li    */
3491591693c7b415e9869157c711fe11263c95d74eDavid Li   const glsl_type *column_type() const
3501591693c7b415e9869157c711fe11263c95d74eDavid Li   {
3511591693c7b415e9869157c711fe11263c95d74eDavid Li      return is_matrix()
3521591693c7b415e9869157c711fe11263c95d74eDavid Li	 ? get_instance(base_type, vector_elements, 1)
3531591693c7b415e9869157c711fe11263c95d74eDavid Li	 : error_type;
3541591693c7b415e9869157c711fe11263c95d74eDavid Li   }
3551591693c7b415e9869157c711fe11263c95d74eDavid Li
3561591693c7b415e9869157c711fe11263c95d74eDavid Li
3571591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
3581591693c7b415e9869157c711fe11263c95d74eDavid Li    * Get the type of a structure field
3591591693c7b415e9869157c711fe11263c95d74eDavid Li    *
3601591693c7b415e9869157c711fe11263c95d74eDavid Li    * \return
3611591693c7b415e9869157c711fe11263c95d74eDavid Li    * Pointer to the type of the named field.  If the type is not a structure
3621591693c7b415e9869157c711fe11263c95d74eDavid Li    * or the named field does not exist, \c glsl_type::error_type is returned.
3631591693c7b415e9869157c711fe11263c95d74eDavid Li    */
3641591693c7b415e9869157c711fe11263c95d74eDavid Li   const glsl_type *field_type(const char *name) const;
3651591693c7b415e9869157c711fe11263c95d74eDavid Li
3661591693c7b415e9869157c711fe11263c95d74eDavid Li
3671591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
3681591693c7b415e9869157c711fe11263c95d74eDavid Li    * Get the location of a filed within a record type
3691591693c7b415e9869157c711fe11263c95d74eDavid Li    */
3701591693c7b415e9869157c711fe11263c95d74eDavid Li   int field_index(const char *name) const;
3711591693c7b415e9869157c711fe11263c95d74eDavid Li
3721591693c7b415e9869157c711fe11263c95d74eDavid Li
3731591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
3741591693c7b415e9869157c711fe11263c95d74eDavid Li    * Query the number of elements in an array type
3751591693c7b415e9869157c711fe11263c95d74eDavid Li    *
3761591693c7b415e9869157c711fe11263c95d74eDavid Li    * \return
3771591693c7b415e9869157c711fe11263c95d74eDavid Li    * The number of elements in the array for array types or -1 for non-array
3781591693c7b415e9869157c711fe11263c95d74eDavid Li    * types.  If the number of elements in the array has not yet been declared,
3791591693c7b415e9869157c711fe11263c95d74eDavid Li    * zero is returned.
3801591693c7b415e9869157c711fe11263c95d74eDavid Li    */
3811591693c7b415e9869157c711fe11263c95d74eDavid Li   int array_size() const
3821591693c7b415e9869157c711fe11263c95d74eDavid Li   {
3831591693c7b415e9869157c711fe11263c95d74eDavid Li      return is_array() ? length : -1;
3841591693c7b415e9869157c711fe11263c95d74eDavid Li   }
3851591693c7b415e9869157c711fe11263c95d74eDavid Li
3861591693c7b415e9869157c711fe11263c95d74eDavid Liprivate:
3871591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
388d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li    * hieralloc context for all glsl_type allocations
3891591693c7b415e9869157c711fe11263c95d74eDavid Li    *
3901591693c7b415e9869157c711fe11263c95d74eDavid Li    * Set on the first call to \c glsl_type::new.
3911591693c7b415e9869157c711fe11263c95d74eDavid Li    */
3921591693c7b415e9869157c711fe11263c95d74eDavid Li   static void *mem_ctx;
3931591693c7b415e9869157c711fe11263c95d74eDavid Li
394d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li   void init_hieralloc_type_ctx(void);
3951591693c7b415e9869157c711fe11263c95d74eDavid Li
3961591693c7b415e9869157c711fe11263c95d74eDavid Li   /** Constructor for vector and matrix types */
3971591693c7b415e9869157c711fe11263c95d74eDavid Li   glsl_type(GLenum gl_type,
3981591693c7b415e9869157c711fe11263c95d74eDavid Li	     glsl_base_type base_type, unsigned vector_elements,
3991591693c7b415e9869157c711fe11263c95d74eDavid Li	     unsigned matrix_columns, const char *name);
4001591693c7b415e9869157c711fe11263c95d74eDavid Li
4011591693c7b415e9869157c711fe11263c95d74eDavid Li   /** Constructor for sampler types */
4021591693c7b415e9869157c711fe11263c95d74eDavid Li   glsl_type(GLenum gl_type,
4031591693c7b415e9869157c711fe11263c95d74eDavid Li	     enum glsl_sampler_dim dim, bool shadow, bool array,
4041591693c7b415e9869157c711fe11263c95d74eDavid Li	     unsigned type, const char *name);
4051591693c7b415e9869157c711fe11263c95d74eDavid Li
4061591693c7b415e9869157c711fe11263c95d74eDavid Li   /** Constructor for record types */
4071591693c7b415e9869157c711fe11263c95d74eDavid Li   glsl_type(const glsl_struct_field *fields, unsigned num_fields,
4081591693c7b415e9869157c711fe11263c95d74eDavid Li	     const char *name);
4091591693c7b415e9869157c711fe11263c95d74eDavid Li
4101591693c7b415e9869157c711fe11263c95d74eDavid Li   /** Constructor for array types */
4111591693c7b415e9869157c711fe11263c95d74eDavid Li   glsl_type(const glsl_type *array, unsigned length);
4121591693c7b415e9869157c711fe11263c95d74eDavid Li
4131591693c7b415e9869157c711fe11263c95d74eDavid Li   /** Hash table containing the known array types. */
4141591693c7b415e9869157c711fe11263c95d74eDavid Li   static struct hash_table *array_types;
4151591693c7b415e9869157c711fe11263c95d74eDavid Li
4161591693c7b415e9869157c711fe11263c95d74eDavid Li   /** Hash table containing the known record types. */
4171591693c7b415e9869157c711fe11263c95d74eDavid Li   static struct hash_table *record_types;
4181591693c7b415e9869157c711fe11263c95d74eDavid Li
4191591693c7b415e9869157c711fe11263c95d74eDavid Li   static int record_key_compare(const void *a, const void *b);
4201591693c7b415e9869157c711fe11263c95d74eDavid Li   static unsigned record_key_hash(const void *key);
4211591693c7b415e9869157c711fe11263c95d74eDavid Li
4221591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
4231591693c7b415e9869157c711fe11263c95d74eDavid Li    * \name Pointers to various type singletons
4241591693c7b415e9869157c711fe11263c95d74eDavid Li    */
4251591693c7b415e9869157c711fe11263c95d74eDavid Li   /*@{*/
4261591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type _error_type;
4271591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type void_type;
4281591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type builtin_core_types[];
4291591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type builtin_structure_types[];
4301591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type builtin_110_deprecated_structure_types[];
4311591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type builtin_110_types[];
4321591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type builtin_120_types[];
4331591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type builtin_130_types[];
4341591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type builtin_ARB_texture_rectangle_types[];
4351591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type builtin_EXT_texture_array_types[];
4361591693c7b415e9869157c711fe11263c95d74eDavid Li   static const glsl_type builtin_EXT_texture_buffer_object_types[];
4371591693c7b415e9869157c711fe11263c95d74eDavid Li   /*@}*/
4381591693c7b415e9869157c711fe11263c95d74eDavid Li
4391591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
4401591693c7b415e9869157c711fe11263c95d74eDavid Li    * \name Methods to populate a symbol table with built-in types.
4411591693c7b415e9869157c711fe11263c95d74eDavid Li    *
4421591693c7b415e9869157c711fe11263c95d74eDavid Li    * \internal
4431591693c7b415e9869157c711fe11263c95d74eDavid Li    * This is one of the truely annoying things about C++.  Methods that are
4441591693c7b415e9869157c711fe11263c95d74eDavid Li    * completely internal and private to a type still have to be advertised to
4451591693c7b415e9869157c711fe11263c95d74eDavid Li    * the world in a public header file.
4461591693c7b415e9869157c711fe11263c95d74eDavid Li    */
4471591693c7b415e9869157c711fe11263c95d74eDavid Li   /*@{*/
4481591693c7b415e9869157c711fe11263c95d74eDavid Li   static void generate_100ES_types(glsl_symbol_table *);
4491591693c7b415e9869157c711fe11263c95d74eDavid Li   static void generate_110_types(glsl_symbol_table *);
4501591693c7b415e9869157c711fe11263c95d74eDavid Li   static void generate_120_types(glsl_symbol_table *);
4511591693c7b415e9869157c711fe11263c95d74eDavid Li   static void generate_130_types(glsl_symbol_table *);
4521591693c7b415e9869157c711fe11263c95d74eDavid Li   static void generate_ARB_texture_rectangle_types(glsl_symbol_table *, bool);
4531591693c7b415e9869157c711fe11263c95d74eDavid Li   static void generate_EXT_texture_array_types(glsl_symbol_table *, bool);
4541591693c7b415e9869157c711fe11263c95d74eDavid Li   /*@}*/
4551591693c7b415e9869157c711fe11263c95d74eDavid Li
4561591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
4571591693c7b415e9869157c711fe11263c95d74eDavid Li    * \name Friend functions.
4581591693c7b415e9869157c711fe11263c95d74eDavid Li    *
4591591693c7b415e9869157c711fe11263c95d74eDavid Li    * These functions are friends because they must have C linkage and the
4601591693c7b415e9869157c711fe11263c95d74eDavid Li    * need to call various private methods or access various private static
4611591693c7b415e9869157c711fe11263c95d74eDavid Li    * data.
4621591693c7b415e9869157c711fe11263c95d74eDavid Li    */
4631591693c7b415e9869157c711fe11263c95d74eDavid Li   /*@{*/
4641591693c7b415e9869157c711fe11263c95d74eDavid Li   friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
4651591693c7b415e9869157c711fe11263c95d74eDavid Li   friend void _mesa_glsl_release_types(void);
4661591693c7b415e9869157c711fe11263c95d74eDavid Li   /*@}*/
4671591693c7b415e9869157c711fe11263c95d74eDavid Li};
4681591693c7b415e9869157c711fe11263c95d74eDavid Li
4691591693c7b415e9869157c711fe11263c95d74eDavid Listruct glsl_struct_field {
4701591693c7b415e9869157c711fe11263c95d74eDavid Li   const struct glsl_type *type;
4711591693c7b415e9869157c711fe11263c95d74eDavid Li   const char *name;
4721591693c7b415e9869157c711fe11263c95d74eDavid Li};
4731591693c7b415e9869157c711fe11263c95d74eDavid Li
4741591693c7b415e9869157c711fe11263c95d74eDavid Li#endif /* GLSL_TYPES_H */
475