Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2009 Intel Corporation
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21.  * DEALINGS IN THE SOFTWARE.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include "main/core.h" /* for Elements, MAX2 */
  26. #include "glsl_parser_extras.h"
  27. #include "glsl_types.h"
  28. #include "program/hash_table.h"
  29.  
  30.  
  31. mtx_t glsl_type::mutex = _MTX_INITIALIZER_NP;
  32. hash_table *glsl_type::array_types = NULL;
  33. hash_table *glsl_type::record_types = NULL;
  34. hash_table *glsl_type::interface_types = NULL;
  35. void *glsl_type::mem_ctx = NULL;
  36.  
  37. void
  38. glsl_type::init_ralloc_type_ctx(void)
  39. {
  40.    if (glsl_type::mem_ctx == NULL) {
  41.       glsl_type::mem_ctx = ralloc_autofree_context();
  42.       assert(glsl_type::mem_ctx != NULL);
  43.    }
  44. }
  45.  
  46. glsl_type::glsl_type(GLenum gl_type,
  47.                      glsl_base_type base_type, unsigned vector_elements,
  48.                      unsigned matrix_columns, const char *name) :
  49.    gl_type(gl_type),
  50.    base_type(base_type),
  51.    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  52.    sampler_type(0), interface_packing(0),
  53.    vector_elements(vector_elements), matrix_columns(matrix_columns),
  54.    length(0)
  55. {
  56.    mtx_lock(&glsl_type::mutex);
  57.  
  58.    init_ralloc_type_ctx();
  59.    assert(name != NULL);
  60.    this->name = ralloc_strdup(this->mem_ctx, name);
  61.  
  62.    mtx_unlock(&glsl_type::mutex);
  63.  
  64.    /* Neither dimension is zero or both dimensions are zero.
  65.     */
  66.    assert((vector_elements == 0) == (matrix_columns == 0));
  67.    memset(& fields, 0, sizeof(fields));
  68. }
  69.  
  70. glsl_type::glsl_type(GLenum gl_type, glsl_base_type base_type,
  71.                      enum glsl_sampler_dim dim, bool shadow, bool array,
  72.                      unsigned type, const char *name) :
  73.    gl_type(gl_type),
  74.    base_type(base_type),
  75.    sampler_dimensionality(dim), sampler_shadow(shadow),
  76.    sampler_array(array), sampler_type(type), interface_packing(0),
  77.    length(0)
  78. {
  79.    mtx_lock(&glsl_type::mutex);
  80.  
  81.    init_ralloc_type_ctx();
  82.    assert(name != NULL);
  83.    this->name = ralloc_strdup(this->mem_ctx, name);
  84.  
  85.    mtx_unlock(&glsl_type::mutex);
  86.  
  87.    memset(& fields, 0, sizeof(fields));
  88.  
  89.    if (base_type == GLSL_TYPE_SAMPLER) {
  90.       /* Samplers take no storage whatsoever. */
  91.       matrix_columns = vector_elements = 0;
  92.    } else {
  93.       matrix_columns = vector_elements = 1;
  94.    }
  95. }
  96.  
  97. glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
  98.                      const char *name) :
  99.    gl_type(0),
  100.    base_type(GLSL_TYPE_STRUCT),
  101.    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  102.    sampler_type(0), interface_packing(0),
  103.    vector_elements(0), matrix_columns(0),
  104.    length(num_fields)
  105. {
  106.    unsigned int i;
  107.  
  108.    mtx_lock(&glsl_type::mutex);
  109.  
  110.    init_ralloc_type_ctx();
  111.    assert(name != NULL);
  112.    this->name = ralloc_strdup(this->mem_ctx, name);
  113.    this->fields.structure = ralloc_array(this->mem_ctx,
  114.                                          glsl_struct_field, length);
  115.  
  116.    for (i = 0; i < length; i++) {
  117.       this->fields.structure[i].type = fields[i].type;
  118.       this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
  119.                                                      fields[i].name);
  120.       this->fields.structure[i].location = fields[i].location;
  121.       this->fields.structure[i].interpolation = fields[i].interpolation;
  122.       this->fields.structure[i].centroid = fields[i].centroid;
  123.       this->fields.structure[i].sample = fields[i].sample;
  124.       this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
  125.    }
  126.  
  127.    mtx_unlock(&glsl_type::mutex);
  128. }
  129.  
  130. glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
  131.                      enum glsl_interface_packing packing, const char *name) :
  132.    gl_type(0),
  133.    base_type(GLSL_TYPE_INTERFACE),
  134.    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  135.    sampler_type(0), interface_packing((unsigned) packing),
  136.    vector_elements(0), matrix_columns(0),
  137.    length(num_fields)
  138. {
  139.    unsigned int i;
  140.  
  141.    mtx_lock(&glsl_type::mutex);
  142.  
  143.    init_ralloc_type_ctx();
  144.    assert(name != NULL);
  145.    this->name = ralloc_strdup(this->mem_ctx, name);
  146.    this->fields.structure = ralloc_array(this->mem_ctx,
  147.                                          glsl_struct_field, length);
  148.    for (i = 0; i < length; i++) {
  149.       this->fields.structure[i].type = fields[i].type;
  150.       this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
  151.                                                      fields[i].name);
  152.       this->fields.structure[i].location = fields[i].location;
  153.       this->fields.structure[i].interpolation = fields[i].interpolation;
  154.       this->fields.structure[i].centroid = fields[i].centroid;
  155.       this->fields.structure[i].sample = fields[i].sample;
  156.       this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
  157.    }
  158.  
  159.    mtx_unlock(&glsl_type::mutex);
  160. }
  161.  
  162.  
  163. bool
  164. glsl_type::contains_sampler() const
  165. {
  166.    if (this->is_array()) {
  167.       return this->fields.array->contains_sampler();
  168.    } else if (this->is_record()) {
  169.       for (unsigned int i = 0; i < this->length; i++) {
  170.          if (this->fields.structure[i].type->contains_sampler())
  171.             return true;
  172.       }
  173.       return false;
  174.    } else {
  175.       return this->is_sampler();
  176.    }
  177. }
  178.  
  179.  
  180. bool
  181. glsl_type::contains_integer() const
  182. {
  183.    if (this->is_array()) {
  184.       return this->fields.array->contains_integer();
  185.    } else if (this->is_record()) {
  186.       for (unsigned int i = 0; i < this->length; i++) {
  187.          if (this->fields.structure[i].type->contains_integer())
  188.             return true;
  189.       }
  190.       return false;
  191.    } else {
  192.       return this->is_integer();
  193.    }
  194. }
  195.  
  196. bool
  197. glsl_type::contains_double() const
  198. {
  199.    if (this->is_array()) {
  200.       return this->fields.array->contains_double();
  201.    } else if (this->is_record()) {
  202.       for (unsigned int i = 0; i < this->length; i++) {
  203.          if (this->fields.structure[i].type->contains_double())
  204.             return true;
  205.       }
  206.       return false;
  207.    } else {
  208.       return this->is_double();
  209.    }
  210. }
  211.  
  212. bool
  213. glsl_type::contains_opaque() const {
  214.    switch (base_type) {
  215.    case GLSL_TYPE_SAMPLER:
  216.    case GLSL_TYPE_IMAGE:
  217.    case GLSL_TYPE_ATOMIC_UINT:
  218.       return true;
  219.    case GLSL_TYPE_ARRAY:
  220.       return element_type()->contains_opaque();
  221.    case GLSL_TYPE_STRUCT:
  222.       for (unsigned int i = 0; i < length; i++) {
  223.          if (fields.structure[i].type->contains_opaque())
  224.             return true;
  225.       }
  226.       return false;
  227.    default:
  228.       return false;
  229.    }
  230. }
  231.  
  232. gl_texture_index
  233. glsl_type::sampler_index() const
  234. {
  235.    const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
  236.  
  237.    assert(t->is_sampler());
  238.  
  239.    switch (t->sampler_dimensionality) {
  240.    case GLSL_SAMPLER_DIM_1D:
  241.       return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
  242.    case GLSL_SAMPLER_DIM_2D:
  243.       return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
  244.    case GLSL_SAMPLER_DIM_3D:
  245.       return TEXTURE_3D_INDEX;
  246.    case GLSL_SAMPLER_DIM_CUBE:
  247.       return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
  248.    case GLSL_SAMPLER_DIM_RECT:
  249.       return TEXTURE_RECT_INDEX;
  250.    case GLSL_SAMPLER_DIM_BUF:
  251.       return TEXTURE_BUFFER_INDEX;
  252.    case GLSL_SAMPLER_DIM_EXTERNAL:
  253.       return TEXTURE_EXTERNAL_INDEX;
  254.    case GLSL_SAMPLER_DIM_MS:
  255.       return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
  256.    default:
  257.       assert(!"Should not get here.");
  258.       return TEXTURE_BUFFER_INDEX;
  259.    }
  260. }
  261.  
  262. bool
  263. glsl_type::contains_image() const
  264. {
  265.    if (this->is_array()) {
  266.       return this->fields.array->contains_image();
  267.    } else if (this->is_record()) {
  268.       for (unsigned int i = 0; i < this->length; i++) {
  269.          if (this->fields.structure[i].type->contains_image())
  270.             return true;
  271.       }
  272.       return false;
  273.    } else {
  274.       return this->is_image();
  275.    }
  276. }
  277.  
  278. const glsl_type *glsl_type::get_base_type() const
  279. {
  280.    switch (base_type) {
  281.    case GLSL_TYPE_UINT:
  282.       return uint_type;
  283.    case GLSL_TYPE_INT:
  284.       return int_type;
  285.    case GLSL_TYPE_FLOAT:
  286.       return float_type;
  287.    case GLSL_TYPE_DOUBLE:
  288.       return double_type;
  289.    case GLSL_TYPE_BOOL:
  290.       return bool_type;
  291.    default:
  292.       return error_type;
  293.    }
  294. }
  295.  
  296.  
  297. const glsl_type *glsl_type::get_scalar_type() const
  298. {
  299.    const glsl_type *type = this;
  300.  
  301.    /* Handle arrays */
  302.    while (type->base_type == GLSL_TYPE_ARRAY)
  303.       type = type->fields.array;
  304.  
  305.    /* Handle vectors and matrices */
  306.    switch (type->base_type) {
  307.    case GLSL_TYPE_UINT:
  308.       return uint_type;
  309.    case GLSL_TYPE_INT:
  310.       return int_type;
  311.    case GLSL_TYPE_FLOAT:
  312.       return float_type;
  313.    case GLSL_TYPE_DOUBLE:
  314.       return double_type;
  315.    case GLSL_TYPE_BOOL:
  316.       return bool_type;
  317.    default:
  318.       /* Handle everything else */
  319.       return type;
  320.    }
  321. }
  322.  
  323.  
  324. void
  325. _mesa_glsl_release_types(void)
  326. {
  327.    mtx_lock(&glsl_type::mutex);
  328.  
  329.    if (glsl_type::array_types != NULL) {
  330.       hash_table_dtor(glsl_type::array_types);
  331.       glsl_type::array_types = NULL;
  332.    }
  333.  
  334.    if (glsl_type::record_types != NULL) {
  335.       hash_table_dtor(glsl_type::record_types);
  336.       glsl_type::record_types = NULL;
  337.    }
  338.  
  339.    mtx_unlock(&glsl_type::mutex);
  340. }
  341.  
  342.  
  343. glsl_type::glsl_type(const glsl_type *array, unsigned length) :
  344.    base_type(GLSL_TYPE_ARRAY),
  345.    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  346.    sampler_type(0), interface_packing(0),
  347.    vector_elements(0), matrix_columns(0),
  348.    length(length), name(NULL)
  349. {
  350.    this->fields.array = array;
  351.    /* Inherit the gl type of the base. The GL type is used for
  352.     * uniform/statevar handling in Mesa and the arrayness of the type
  353.     * is represented by the size rather than the type.
  354.     */
  355.    this->gl_type = array->gl_type;
  356.  
  357.    /* Allow a maximum of 10 characters for the array size.  This is enough
  358.     * for 32-bits of ~0.  The extra 3 are for the '[', ']', and terminating
  359.     * NUL.
  360.     */
  361.    const unsigned name_length = strlen(array->name) + 10 + 3;
  362.  
  363.    mtx_lock(&glsl_type::mutex);
  364.    char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
  365.    mtx_unlock(&glsl_type::mutex);
  366.  
  367.    if (length == 0)
  368.       snprintf(n, name_length, "%s[]", array->name);
  369.    else {
  370.       /* insert outermost dimensions in the correct spot
  371.        * otherwise the dimension order will be backwards
  372.        */
  373.       const char *pos = strchr(array->name, '[');
  374.       if (pos) {
  375.          int idx = pos - array->name;
  376.          snprintf(n, idx+1, "%s", array->name);
  377.          snprintf(n + idx, name_length - idx, "[%u]%s",
  378.                   length, array->name + idx);
  379.       } else {
  380.          snprintf(n, name_length, "%s[%u]", array->name, length);
  381.       }
  382.    }
  383.  
  384.    this->name = n;
  385. }
  386.  
  387.  
  388. const glsl_type *
  389. glsl_type::vec(unsigned components)
  390. {
  391.    if (components == 0 || components > 4)
  392.       return error_type;
  393.  
  394.    static const glsl_type *const ts[] = {
  395.       float_type, vec2_type, vec3_type, vec4_type
  396.    };
  397.    return ts[components - 1];
  398. }
  399.  
  400. const glsl_type *
  401. glsl_type::dvec(unsigned components)
  402. {
  403.    if (components == 0 || components > 4)
  404.       return error_type;
  405.  
  406.    static const glsl_type *const ts[] = {
  407.       double_type, dvec2_type, dvec3_type, dvec4_type
  408.    };
  409.    return ts[components - 1];
  410. }
  411.  
  412. const glsl_type *
  413. glsl_type::ivec(unsigned components)
  414. {
  415.    if (components == 0 || components > 4)
  416.       return error_type;
  417.  
  418.    static const glsl_type *const ts[] = {
  419.       int_type, ivec2_type, ivec3_type, ivec4_type
  420.    };
  421.    return ts[components - 1];
  422. }
  423.  
  424.  
  425. const glsl_type *
  426. glsl_type::uvec(unsigned components)
  427. {
  428.    if (components == 0 || components > 4)
  429.       return error_type;
  430.  
  431.    static const glsl_type *const ts[] = {
  432.       uint_type, uvec2_type, uvec3_type, uvec4_type
  433.    };
  434.    return ts[components - 1];
  435. }
  436.  
  437.  
  438. const glsl_type *
  439. glsl_type::bvec(unsigned components)
  440. {
  441.    if (components == 0 || components > 4)
  442.       return error_type;
  443.  
  444.    static const glsl_type *const ts[] = {
  445.       bool_type, bvec2_type, bvec3_type, bvec4_type
  446.    };
  447.    return ts[components - 1];
  448. }
  449.  
  450.  
  451. const glsl_type *
  452. glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
  453. {
  454.    if (base_type == GLSL_TYPE_VOID)
  455.       return void_type;
  456.  
  457.    if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
  458.       return error_type;
  459.  
  460.    /* Treat GLSL vectors as Nx1 matrices.
  461.     */
  462.    if (columns == 1) {
  463.       switch (base_type) {
  464.       case GLSL_TYPE_UINT:
  465.          return uvec(rows);
  466.       case GLSL_TYPE_INT:
  467.          return ivec(rows);
  468.       case GLSL_TYPE_FLOAT:
  469.          return vec(rows);
  470.       case GLSL_TYPE_DOUBLE:
  471.          return dvec(rows);
  472.       case GLSL_TYPE_BOOL:
  473.          return bvec(rows);
  474.       default:
  475.          return error_type;
  476.       }
  477.    } else {
  478.       if ((base_type != GLSL_TYPE_FLOAT && base_type != GLSL_TYPE_DOUBLE) || (rows == 1))
  479.          return error_type;
  480.  
  481.       /* GLSL matrix types are named mat{COLUMNS}x{ROWS}.  Only the following
  482.        * combinations are valid:
  483.        *
  484.        *   1 2 3 4
  485.        * 1
  486.        * 2   x x x
  487.        * 3   x x x
  488.        * 4   x x x
  489.        */
  490. #define IDX(c,r) (((c-1)*3) + (r-1))
  491.  
  492.       if (base_type == GLSL_TYPE_DOUBLE) {
  493.          switch (IDX(columns, rows)) {
  494.          case IDX(2,2): return dmat2_type;
  495.          case IDX(2,3): return dmat2x3_type;
  496.          case IDX(2,4): return dmat2x4_type;
  497.          case IDX(3,2): return dmat3x2_type;
  498.          case IDX(3,3): return dmat3_type;
  499.          case IDX(3,4): return dmat3x4_type;
  500.          case IDX(4,2): return dmat4x2_type;
  501.          case IDX(4,3): return dmat4x3_type;
  502.          case IDX(4,4): return dmat4_type;
  503.          default: return error_type;
  504.          }
  505.       } else {
  506.          switch (IDX(columns, rows)) {
  507.          case IDX(2,2): return mat2_type;
  508.          case IDX(2,3): return mat2x3_type;
  509.          case IDX(2,4): return mat2x4_type;
  510.          case IDX(3,2): return mat3x2_type;
  511.          case IDX(3,3): return mat3_type;
  512.          case IDX(3,4): return mat3x4_type;
  513.          case IDX(4,2): return mat4x2_type;
  514.          case IDX(4,3): return mat4x3_type;
  515.          case IDX(4,4): return mat4_type;
  516.          default: return error_type;
  517.          }
  518.       }
  519.    }
  520.  
  521.    assert(!"Should not get here.");
  522.    return error_type;
  523. }
  524.  
  525. const glsl_type *
  526. glsl_type::get_sampler_instance(enum glsl_sampler_dim dim,
  527.                                 bool shadow,
  528.                                 bool array,
  529.                                 glsl_base_type type)
  530. {
  531.    switch (type) {
  532.    case GLSL_TYPE_FLOAT:
  533.       switch (dim) {
  534.       case GLSL_SAMPLER_DIM_1D:
  535.          if (shadow)
  536.             return (array ? sampler1DArrayShadow_type : sampler1DShadow_type);
  537.          else
  538.             return (array ? sampler1DArray_type : sampler1D_type);
  539.       case GLSL_SAMPLER_DIM_2D:
  540.          if (shadow)
  541.             return (array ? sampler2DArrayShadow_type : sampler2DShadow_type);
  542.          else
  543.             return (array ? sampler2DArray_type : sampler2D_type);
  544.       case GLSL_SAMPLER_DIM_3D:
  545.          if (shadow || array)
  546.             return error_type;
  547.          else
  548.             return sampler3D_type;
  549.       case GLSL_SAMPLER_DIM_CUBE:
  550.          if (shadow)
  551.             return (array ? samplerCubeArrayShadow_type : samplerCubeShadow_type);
  552.          else
  553.             return (array ? samplerCubeArray_type : samplerCube_type);
  554.       case GLSL_SAMPLER_DIM_RECT:
  555.          if (array)
  556.             return error_type;
  557.          if (shadow)
  558.             return sampler2DRectShadow_type;
  559.          else
  560.             return sampler2DRect_type;
  561.       case GLSL_SAMPLER_DIM_BUF:
  562.          if (shadow || array)
  563.             return error_type;
  564.          else
  565.             return samplerBuffer_type;
  566.       case GLSL_SAMPLER_DIM_MS:
  567.          if (shadow)
  568.             return error_type;
  569.          return (array ? sampler2DMSArray_type : sampler2DMS_type);
  570.       case GLSL_SAMPLER_DIM_EXTERNAL:
  571.          if (shadow || array)
  572.             return error_type;
  573.          else
  574.             return samplerExternalOES_type;
  575.       }
  576.    case GLSL_TYPE_INT:
  577.       if (shadow)
  578.          return error_type;
  579.       switch (dim) {
  580.       case GLSL_SAMPLER_DIM_1D:
  581.          return (array ? isampler1DArray_type : isampler1D_type);
  582.       case GLSL_SAMPLER_DIM_2D:
  583.          return (array ? isampler2DArray_type : isampler2D_type);
  584.       case GLSL_SAMPLER_DIM_3D:
  585.          if (array)
  586.             return error_type;
  587.          return isampler3D_type;
  588.       case GLSL_SAMPLER_DIM_CUBE:
  589.          return (array ? isamplerCubeArray_type : isamplerCube_type);
  590.       case GLSL_SAMPLER_DIM_RECT:
  591.          if (array)
  592.             return error_type;
  593.          return isampler2DRect_type;
  594.       case GLSL_SAMPLER_DIM_BUF:
  595.          if (array)
  596.             return error_type;
  597.          return isamplerBuffer_type;
  598.       case GLSL_SAMPLER_DIM_MS:
  599.          return (array ? isampler2DMSArray_type : isampler2DMS_type);
  600.       case GLSL_SAMPLER_DIM_EXTERNAL:
  601.          return error_type;
  602.       }
  603.    case GLSL_TYPE_UINT:
  604.       if (shadow)
  605.          return error_type;
  606.       switch (dim) {
  607.       case GLSL_SAMPLER_DIM_1D:
  608.          return (array ? usampler1DArray_type : usampler1D_type);
  609.       case GLSL_SAMPLER_DIM_2D:
  610.          return (array ? usampler2DArray_type : usampler2D_type);
  611.       case GLSL_SAMPLER_DIM_3D:
  612.          if (array)
  613.             return error_type;
  614.          return usampler3D_type;
  615.       case GLSL_SAMPLER_DIM_CUBE:
  616.          return (array ? usamplerCubeArray_type : usamplerCube_type);
  617.       case GLSL_SAMPLER_DIM_RECT:
  618.          if (array)
  619.             return error_type;
  620.          return usampler2DRect_type;
  621.       case GLSL_SAMPLER_DIM_BUF:
  622.          if (array)
  623.             return error_type;
  624.          return usamplerBuffer_type;
  625.       case GLSL_SAMPLER_DIM_MS:
  626.          return (array ? usampler2DMSArray_type : usampler2DMS_type);
  627.       case GLSL_SAMPLER_DIM_EXTERNAL:
  628.          return error_type;
  629.       }
  630.    default:
  631.       return error_type;
  632.    }
  633.  
  634.    unreachable("switch statement above should be complete");
  635. }
  636.  
  637. const glsl_type *
  638. glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
  639. {
  640.    /* Generate a name using the base type pointer in the key.  This is
  641.     * done because the name of the base type may not be unique across
  642.     * shaders.  For example, two shaders may have different record types
  643.     * named 'foo'.
  644.     */
  645.    char key[128];
  646.    snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
  647.  
  648.    mtx_lock(&glsl_type::mutex);
  649.  
  650.    if (array_types == NULL) {
  651.       array_types = hash_table_ctor(64, hash_table_string_hash,
  652.                                     hash_table_string_compare);
  653.    }
  654.  
  655.    const glsl_type *t = (glsl_type *) hash_table_find(array_types, key);
  656.  
  657.    if (t == NULL) {
  658.       mtx_unlock(&glsl_type::mutex);
  659.       t = new glsl_type(base, array_size);
  660.       mtx_lock(&glsl_type::mutex);
  661.  
  662.       hash_table_insert(array_types, (void *) t, ralloc_strdup(mem_ctx, key));
  663.    }
  664.  
  665.    assert(t->base_type == GLSL_TYPE_ARRAY);
  666.    assert(t->length == array_size);
  667.    assert(t->fields.array == base);
  668.  
  669.    mtx_unlock(&glsl_type::mutex);
  670.  
  671.    return t;
  672. }
  673.  
  674.  
  675. bool
  676. glsl_type::record_compare(const glsl_type *b) const
  677. {
  678.    if (this->length != b->length)
  679.       return false;
  680.  
  681.    if (this->interface_packing != b->interface_packing)
  682.       return false;
  683.  
  684.    /* From the GLSL 4.20 specification (Sec 4.2):
  685.     *
  686.     *     "Structures must have the same name, sequence of type names, and
  687.     *     type definitions, and field names to be considered the same type."
  688.     *
  689.     * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
  690.     *
  691.     * Note that we cannot force type name check when comparing unnamed
  692.     * structure types, these have a unique name assigned during parsing.
  693.     */
  694.    if (!this->is_anonymous() && !b->is_anonymous())
  695.       if (strcmp(this->name, b->name) != 0)
  696.          return false;
  697.  
  698.    for (unsigned i = 0; i < this->length; i++) {
  699.       if (this->fields.structure[i].type != b->fields.structure[i].type)
  700.          return false;
  701.       if (strcmp(this->fields.structure[i].name,
  702.                  b->fields.structure[i].name) != 0)
  703.          return false;
  704.       if (this->fields.structure[i].matrix_layout
  705.          != b->fields.structure[i].matrix_layout)
  706.         return false;
  707.       if (this->fields.structure[i].location
  708.           != b->fields.structure[i].location)
  709.          return false;
  710.       if (this->fields.structure[i].interpolation
  711.           != b->fields.structure[i].interpolation)
  712.          return false;
  713.       if (this->fields.structure[i].centroid
  714.           != b->fields.structure[i].centroid)
  715.          return false;
  716.       if (this->fields.structure[i].sample
  717.           != b->fields.structure[i].sample)
  718.          return false;
  719.    }
  720.  
  721.    return true;
  722. }
  723.  
  724.  
  725. int
  726. glsl_type::record_key_compare(const void *a, const void *b)
  727. {
  728.    const glsl_type *const key1 = (glsl_type *) a;
  729.    const glsl_type *const key2 = (glsl_type *) b;
  730.  
  731.    /* Return zero is the types match (there is zero difference) or non-zero
  732.     * otherwise.
  733.     */
  734.    if (strcmp(key1->name, key2->name) != 0)
  735.       return 1;
  736.  
  737.    return !key1->record_compare(key2);
  738. }
  739.  
  740.  
  741. /**
  742.  * Generate an integer hash value for a glsl_type structure type.
  743.  */
  744. unsigned
  745. glsl_type::record_key_hash(const void *a)
  746. {
  747.    const glsl_type *const key = (glsl_type *) a;
  748.    uintptr_t hash = key->length;
  749.    unsigned retval;
  750.  
  751.    for (unsigned i = 0; i < key->length; i++) {
  752.       /* casting pointer to uintptr_t */
  753.       hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
  754.    }
  755.  
  756.    if (sizeof(hash) == 8)
  757.       retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
  758.    else
  759.       retval = hash;
  760.  
  761.    return retval;
  762. }
  763.  
  764.  
  765. const glsl_type *
  766. glsl_type::get_record_instance(const glsl_struct_field *fields,
  767.                                unsigned num_fields,
  768.                                const char *name)
  769. {
  770.    const glsl_type key(fields, num_fields, name);
  771.  
  772.    mtx_lock(&glsl_type::mutex);
  773.  
  774.    if (record_types == NULL) {
  775.       record_types = hash_table_ctor(64, record_key_hash, record_key_compare);
  776.    }
  777.  
  778.    const glsl_type *t = (glsl_type *) hash_table_find(record_types, & key);
  779.    if (t == NULL) {
  780.       mtx_unlock(&glsl_type::mutex);
  781.       t = new glsl_type(fields, num_fields, name);
  782.       mtx_lock(&glsl_type::mutex);
  783.  
  784.       hash_table_insert(record_types, (void *) t, t);
  785.    }
  786.  
  787.    assert(t->base_type == GLSL_TYPE_STRUCT);
  788.    assert(t->length == num_fields);
  789.    assert(strcmp(t->name, name) == 0);
  790.  
  791.    mtx_unlock(&glsl_type::mutex);
  792.  
  793.    return t;
  794. }
  795.  
  796.  
  797. const glsl_type *
  798. glsl_type::get_interface_instance(const glsl_struct_field *fields,
  799.                                   unsigned num_fields,
  800.                                   enum glsl_interface_packing packing,
  801.                                   const char *block_name)
  802. {
  803.    const glsl_type key(fields, num_fields, packing, block_name);
  804.  
  805.    mtx_lock(&glsl_type::mutex);
  806.  
  807.    if (interface_types == NULL) {
  808.       interface_types = hash_table_ctor(64, record_key_hash, record_key_compare);
  809.    }
  810.  
  811.    const glsl_type *t = (glsl_type *) hash_table_find(interface_types, & key);
  812.    if (t == NULL) {
  813.       mtx_unlock(&glsl_type::mutex);
  814.       t = new glsl_type(fields, num_fields, packing, block_name);
  815.       mtx_lock(&glsl_type::mutex);
  816.  
  817.       hash_table_insert(interface_types, (void *) t, t);
  818.    }
  819.  
  820.    assert(t->base_type == GLSL_TYPE_INTERFACE);
  821.    assert(t->length == num_fields);
  822.    assert(strcmp(t->name, block_name) == 0);
  823.  
  824.    mtx_unlock(&glsl_type::mutex);
  825.  
  826.    return t;
  827. }
  828.  
  829.  
  830. const glsl_type *
  831. glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
  832. {
  833.    if (type_a == type_b) {
  834.       return type_a;
  835.    } else if (type_a->is_matrix() && type_b->is_matrix()) {
  836.       /* Matrix multiply.  The columns of A must match the rows of B.  Given
  837.        * the other previously tested constraints, this means the vector type
  838.        * of a row from A must be the same as the vector type of a column from
  839.        * B.
  840.        */
  841.       if (type_a->row_type() == type_b->column_type()) {
  842.          /* The resulting matrix has the number of columns of matrix B and
  843.           * the number of rows of matrix A.  We get the row count of A by
  844.           * looking at the size of a vector that makes up a column.  The
  845.           * transpose (size of a row) is done for B.
  846.           */
  847.          const glsl_type *const type =
  848.             get_instance(type_a->base_type,
  849.                          type_a->column_type()->vector_elements,
  850.                          type_b->row_type()->vector_elements);
  851.          assert(type != error_type);
  852.  
  853.          return type;
  854.       }
  855.    } else if (type_a->is_matrix()) {
  856.       /* A is a matrix and B is a column vector.  Columns of A must match
  857.        * rows of B.  Given the other previously tested constraints, this
  858.        * means the vector type of a row from A must be the same as the
  859.        * vector the type of B.
  860.        */
  861.       if (type_a->row_type() == type_b) {
  862.          /* The resulting vector has a number of elements equal to
  863.           * the number of rows of matrix A. */
  864.          const glsl_type *const type =
  865.             get_instance(type_a->base_type,
  866.                          type_a->column_type()->vector_elements,
  867.                          1);
  868.          assert(type != error_type);
  869.  
  870.          return type;
  871.       }
  872.    } else {
  873.       assert(type_b->is_matrix());
  874.  
  875.       /* A is a row vector and B is a matrix.  Columns of A must match rows
  876.        * of B.  Given the other previously tested constraints, this means
  877.        * the type of A must be the same as the vector type of a column from
  878.        * B.
  879.        */
  880.       if (type_a == type_b->column_type()) {
  881.          /* The resulting vector has a number of elements equal to
  882.           * the number of columns of matrix B. */
  883.          const glsl_type *const type =
  884.             get_instance(type_a->base_type,
  885.                          type_b->row_type()->vector_elements,
  886.                          1);
  887.          assert(type != error_type);
  888.  
  889.          return type;
  890.       }
  891.    }
  892.  
  893.    return error_type;
  894. }
  895.  
  896.  
  897. const glsl_type *
  898. glsl_type::field_type(const char *name) const
  899. {
  900.    if (this->base_type != GLSL_TYPE_STRUCT
  901.        && this->base_type != GLSL_TYPE_INTERFACE)
  902.       return error_type;
  903.  
  904.    for (unsigned i = 0; i < this->length; i++) {
  905.       if (strcmp(name, this->fields.structure[i].name) == 0)
  906.          return this->fields.structure[i].type;
  907.    }
  908.  
  909.    return error_type;
  910. }
  911.  
  912.  
  913. int
  914. glsl_type::field_index(const char *name) const
  915. {
  916.    if (this->base_type != GLSL_TYPE_STRUCT
  917.        && this->base_type != GLSL_TYPE_INTERFACE)
  918.       return -1;
  919.  
  920.    for (unsigned i = 0; i < this->length; i++) {
  921.       if (strcmp(name, this->fields.structure[i].name) == 0)
  922.          return i;
  923.    }
  924.  
  925.    return -1;
  926. }
  927.  
  928.  
  929. unsigned
  930. glsl_type::component_slots() const
  931. {
  932.    switch (this->base_type) {
  933.    case GLSL_TYPE_UINT:
  934.    case GLSL_TYPE_INT:
  935.    case GLSL_TYPE_FLOAT:
  936.    case GLSL_TYPE_BOOL:
  937.       return this->components();
  938.  
  939.    case GLSL_TYPE_DOUBLE:
  940.       return 2 * this->components();
  941.  
  942.    case GLSL_TYPE_STRUCT:
  943.    case GLSL_TYPE_INTERFACE: {
  944.       unsigned size = 0;
  945.  
  946.       for (unsigned i = 0; i < this->length; i++)
  947.          size += this->fields.structure[i].type->component_slots();
  948.  
  949.       return size;
  950.    }
  951.  
  952.    case GLSL_TYPE_ARRAY:
  953.       return this->length * this->fields.array->component_slots();
  954.  
  955.    case GLSL_TYPE_IMAGE:
  956.       return 1;
  957.  
  958.    case GLSL_TYPE_SAMPLER:
  959.    case GLSL_TYPE_ATOMIC_UINT:
  960.    case GLSL_TYPE_VOID:
  961.    case GLSL_TYPE_ERROR:
  962.       break;
  963.    }
  964.  
  965.    return 0;
  966. }
  967.  
  968. unsigned
  969. glsl_type::uniform_locations() const
  970. {
  971.    unsigned size = 0;
  972.  
  973.    switch (this->base_type) {
  974.    case GLSL_TYPE_UINT:
  975.    case GLSL_TYPE_INT:
  976.    case GLSL_TYPE_FLOAT:
  977.    case GLSL_TYPE_DOUBLE:
  978.    case GLSL_TYPE_BOOL:
  979.    case GLSL_TYPE_SAMPLER:
  980.    case GLSL_TYPE_IMAGE:
  981.       return 1;
  982.  
  983.    case GLSL_TYPE_STRUCT:
  984.    case GLSL_TYPE_INTERFACE:
  985.       for (unsigned i = 0; i < this->length; i++)
  986.          size += this->fields.structure[i].type->uniform_locations();
  987.       return size;
  988.    case GLSL_TYPE_ARRAY:
  989.       return this->length * this->fields.array->uniform_locations();
  990.    default:
  991.       return 0;
  992.    }
  993. }
  994.  
  995. bool
  996. glsl_type::can_implicitly_convert_to(const glsl_type *desired,
  997.                                      _mesa_glsl_parse_state *state) const
  998. {
  999.    if (this == desired)
  1000.       return true;
  1001.  
  1002.    /* There is no conversion among matrix types. */
  1003.    if (this->matrix_columns > 1 || desired->matrix_columns > 1)
  1004.       return false;
  1005.  
  1006.    /* Vector size must match. */
  1007.    if (this->vector_elements != desired->vector_elements)
  1008.       return false;
  1009.  
  1010.    /* int and uint can be converted to float. */
  1011.    if (desired->is_float() && this->is_integer())
  1012.       return true;
  1013.  
  1014.    /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
  1015.     * Note that state may be NULL here, when resolving function calls in the
  1016.     * linker. By this time, all the state-dependent checks have already
  1017.     * happened though, so allow anything that's allowed in any shader version. */
  1018.    if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) &&
  1019.          desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
  1020.       return true;
  1021.  
  1022.    /* No implicit conversions from double. */
  1023.    if ((!state || state->has_double()) && this->is_double())
  1024.       return false;
  1025.  
  1026.    /* Conversions from different types to double. */
  1027.    if ((!state || state->has_double()) && desired->is_double()) {
  1028.       if (this->is_float())
  1029.          return true;
  1030.       if (this->is_integer())
  1031.          return true;
  1032.    }
  1033.  
  1034.    return false;
  1035. }
  1036.  
  1037. unsigned
  1038. glsl_type::std140_base_alignment(bool row_major) const
  1039. {
  1040.    unsigned N = is_double() ? 8 : 4;
  1041.  
  1042.    /* (1) If the member is a scalar consuming <N> basic machine units, the
  1043.     *     base alignment is <N>.
  1044.     *
  1045.     * (2) If the member is a two- or four-component vector with components
  1046.     *     consuming <N> basic machine units, the base alignment is 2<N> or
  1047.     *     4<N>, respectively.
  1048.     *
  1049.     * (3) If the member is a three-component vector with components consuming
  1050.     *     <N> basic machine units, the base alignment is 4<N>.
  1051.     */
  1052.    if (this->is_scalar() || this->is_vector()) {
  1053.       switch (this->vector_elements) {
  1054.       case 1:
  1055.          return N;
  1056.       case 2:
  1057.          return 2 * N;
  1058.       case 3:
  1059.       case 4:
  1060.          return 4 * N;
  1061.       }
  1062.    }
  1063.  
  1064.    /* (4) If the member is an array of scalars or vectors, the base alignment
  1065.     *     and array stride are set to match the base alignment of a single
  1066.     *     array element, according to rules (1), (2), and (3), and rounded up
  1067.     *     to the base alignment of a vec4. The array may have padding at the
  1068.     *     end; the base offset of the member following the array is rounded up
  1069.     *     to the next multiple of the base alignment.
  1070.     *
  1071.     * (6) If the member is an array of <S> column-major matrices with <C>
  1072.     *     columns and <R> rows, the matrix is stored identically to a row of
  1073.     *     <S>*<C> column vectors with <R> components each, according to rule
  1074.     *     (4).
  1075.     *
  1076.     * (8) If the member is an array of <S> row-major matrices with <C> columns
  1077.     *     and <R> rows, the matrix is stored identically to a row of <S>*<R>
  1078.     *     row vectors with <C> components each, according to rule (4).
  1079.     *
  1080.     * (10) If the member is an array of <S> structures, the <S> elements of
  1081.     *      the array are laid out in order, according to rule (9).
  1082.     */
  1083.    if (this->is_array()) {
  1084.       if (this->fields.array->is_scalar() ||
  1085.           this->fields.array->is_vector() ||
  1086.           this->fields.array->is_matrix()) {
  1087.          return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
  1088.       } else {
  1089.          assert(this->fields.array->is_record());
  1090.          return this->fields.array->std140_base_alignment(row_major);
  1091.       }
  1092.    }
  1093.  
  1094.    /* (5) If the member is a column-major matrix with <C> columns and
  1095.     *     <R> rows, the matrix is stored identically to an array of
  1096.     *     <C> column vectors with <R> components each, according to
  1097.     *     rule (4).
  1098.     *
  1099.     * (7) If the member is a row-major matrix with <C> columns and <R>
  1100.     *     rows, the matrix is stored identically to an array of <R>
  1101.     *     row vectors with <C> components each, according to rule (4).
  1102.     */
  1103.    if (this->is_matrix()) {
  1104.       const struct glsl_type *vec_type, *array_type;
  1105.       int c = this->matrix_columns;
  1106.       int r = this->vector_elements;
  1107.  
  1108.       if (row_major) {
  1109.          vec_type = get_instance(base_type, c, 1);
  1110.          array_type = glsl_type::get_array_instance(vec_type, r);
  1111.       } else {
  1112.          vec_type = get_instance(base_type, r, 1);
  1113.          array_type = glsl_type::get_array_instance(vec_type, c);
  1114.       }
  1115.  
  1116.       return array_type->std140_base_alignment(false);
  1117.    }
  1118.  
  1119.    /* (9) If the member is a structure, the base alignment of the
  1120.     *     structure is <N>, where <N> is the largest base alignment
  1121.     *     value of any of its members, and rounded up to the base
  1122.     *     alignment of a vec4. The individual members of this
  1123.     *     sub-structure are then assigned offsets by applying this set
  1124.     *     of rules recursively, where the base offset of the first
  1125.     *     member of the sub-structure is equal to the aligned offset
  1126.     *     of the structure. The structure may have padding at the end;
  1127.     *     the base offset of the member following the sub-structure is
  1128.     *     rounded up to the next multiple of the base alignment of the
  1129.     *     structure.
  1130.     */
  1131.    if (this->is_record()) {
  1132.       unsigned base_alignment = 16;
  1133.       for (unsigned i = 0; i < this->length; i++) {
  1134.          bool field_row_major = row_major;
  1135.          const enum glsl_matrix_layout matrix_layout =
  1136.             glsl_matrix_layout(this->fields.structure[i].matrix_layout);
  1137.          if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
  1138.             field_row_major = true;
  1139.          } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
  1140.             field_row_major = false;
  1141.          }
  1142.  
  1143.          const struct glsl_type *field_type = this->fields.structure[i].type;
  1144.          base_alignment = MAX2(base_alignment,
  1145.                                field_type->std140_base_alignment(field_row_major));
  1146.       }
  1147.       return base_alignment;
  1148.    }
  1149.  
  1150.    assert(!"not reached");
  1151.    return -1;
  1152. }
  1153.  
  1154. unsigned
  1155. glsl_type::std140_size(bool row_major) const
  1156. {
  1157.    unsigned N = is_double() ? 8 : 4;
  1158.  
  1159.    /* (1) If the member is a scalar consuming <N> basic machine units, the
  1160.     *     base alignment is <N>.
  1161.     *
  1162.     * (2) If the member is a two- or four-component vector with components
  1163.     *     consuming <N> basic machine units, the base alignment is 2<N> or
  1164.     *     4<N>, respectively.
  1165.     *
  1166.     * (3) If the member is a three-component vector with components consuming
  1167.     *     <N> basic machine units, the base alignment is 4<N>.
  1168.     */
  1169.    if (this->is_scalar() || this->is_vector()) {
  1170.       return this->vector_elements * N;
  1171.    }
  1172.  
  1173.    /* (5) If the member is a column-major matrix with <C> columns and
  1174.     *     <R> rows, the matrix is stored identically to an array of
  1175.     *     <C> column vectors with <R> components each, according to
  1176.     *     rule (4).
  1177.     *
  1178.     * (6) If the member is an array of <S> column-major matrices with <C>
  1179.     *     columns and <R> rows, the matrix is stored identically to a row of
  1180.     *     <S>*<C> column vectors with <R> components each, according to rule
  1181.     *     (4).
  1182.     *
  1183.     * (7) If the member is a row-major matrix with <C> columns and <R>
  1184.     *     rows, the matrix is stored identically to an array of <R>
  1185.     *     row vectors with <C> components each, according to rule (4).
  1186.     *
  1187.     * (8) If the member is an array of <S> row-major matrices with <C> columns
  1188.     *     and <R> rows, the matrix is stored identically to a row of <S>*<R>
  1189.     *     row vectors with <C> components each, according to rule (4).
  1190.     */
  1191.    if (this->without_array()->is_matrix()) {
  1192.       const struct glsl_type *element_type;
  1193.       const struct glsl_type *vec_type;
  1194.       unsigned int array_len;
  1195.  
  1196.       if (this->is_array()) {
  1197.          element_type = this->fields.array;
  1198.          array_len = this->length;
  1199.       } else {
  1200.          element_type = this;
  1201.          array_len = 1;
  1202.       }
  1203.  
  1204.       if (row_major) {
  1205.          vec_type = get_instance(element_type->base_type,
  1206.                                  element_type->matrix_columns, 1);
  1207.  
  1208.          array_len *= element_type->vector_elements;
  1209.       } else {
  1210.          vec_type = get_instance(element_type->base_type,
  1211.                                  element_type->vector_elements, 1);
  1212.          array_len *= element_type->matrix_columns;
  1213.       }
  1214.       const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
  1215.                                                                   array_len);
  1216.  
  1217.       return array_type->std140_size(false);
  1218.    }
  1219.  
  1220.    /* (4) If the member is an array of scalars or vectors, the base alignment
  1221.     *     and array stride are set to match the base alignment of a single
  1222.     *     array element, according to rules (1), (2), and (3), and rounded up
  1223.     *     to the base alignment of a vec4. The array may have padding at the
  1224.     *     end; the base offset of the member following the array is rounded up
  1225.     *     to the next multiple of the base alignment.
  1226.     *
  1227.     * (10) If the member is an array of <S> structures, the <S> elements of
  1228.     *      the array are laid out in order, according to rule (9).
  1229.     */
  1230.    if (this->is_array()) {
  1231.       if (this->fields.array->is_record()) {
  1232.          return this->length * this->fields.array->std140_size(row_major);
  1233.       } else {
  1234.          unsigned element_base_align =
  1235.             this->fields.array->std140_base_alignment(row_major);
  1236.          return this->length * MAX2(element_base_align, 16);
  1237.       }
  1238.    }
  1239.  
  1240.    /* (9) If the member is a structure, the base alignment of the
  1241.     *     structure is <N>, where <N> is the largest base alignment
  1242.     *     value of any of its members, and rounded up to the base
  1243.     *     alignment of a vec4. The individual members of this
  1244.     *     sub-structure are then assigned offsets by applying this set
  1245.     *     of rules recursively, where the base offset of the first
  1246.     *     member of the sub-structure is equal to the aligned offset
  1247.     *     of the structure. The structure may have padding at the end;
  1248.     *     the base offset of the member following the sub-structure is
  1249.     *     rounded up to the next multiple of the base alignment of the
  1250.     *     structure.
  1251.     */
  1252.    if (this->is_record()) {
  1253.       unsigned size = 0;
  1254.       unsigned max_align = 0;
  1255.  
  1256.       for (unsigned i = 0; i < this->length; i++) {
  1257.          bool field_row_major = row_major;
  1258.          const enum glsl_matrix_layout matrix_layout =
  1259.             glsl_matrix_layout(this->fields.structure[i].matrix_layout);
  1260.          if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
  1261.             field_row_major = true;
  1262.          } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
  1263.             field_row_major = false;
  1264.          }
  1265.  
  1266.          const struct glsl_type *field_type = this->fields.structure[i].type;
  1267.          unsigned align = field_type->std140_base_alignment(field_row_major);
  1268.          size = glsl_align(size, align);
  1269.          size += field_type->std140_size(field_row_major);
  1270.  
  1271.          max_align = MAX2(align, max_align);
  1272.  
  1273.          if (field_type->is_record() && (i + 1 < this->length))
  1274.             size = glsl_align(size, 16);
  1275.       }
  1276.       size = glsl_align(size, MAX2(max_align, 16));
  1277.       return size;
  1278.    }
  1279.  
  1280.    assert(!"not reached");
  1281.    return -1;
  1282. }
  1283.  
  1284.  
  1285. unsigned
  1286. glsl_type::count_attribute_slots() const
  1287. {
  1288.    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
  1289.     *
  1290.     *     "A scalar input counts the same amount against this limit as a vec4,
  1291.     *     so applications may want to consider packing groups of four
  1292.     *     unrelated float inputs together into a vector to better utilize the
  1293.     *     capabilities of the underlying hardware. A matrix input will use up
  1294.     *     multiple locations.  The number of locations used will equal the
  1295.     *     number of columns in the matrix."
  1296.     *
  1297.     * The spec does not explicitly say how arrays are counted.  However, it
  1298.     * should be safe to assume the total number of slots consumed by an array
  1299.     * is the number of entries in the array multiplied by the number of slots
  1300.     * consumed by a single element of the array.
  1301.     *
  1302.     * The spec says nothing about how structs are counted, because vertex
  1303.     * attributes are not allowed to be (or contain) structs.  However, Mesa
  1304.     * allows varying structs, the number of varying slots taken up by a
  1305.     * varying struct is simply equal to the sum of the number of slots taken
  1306.     * up by each element.
  1307.     */
  1308.    switch (this->base_type) {
  1309.    case GLSL_TYPE_UINT:
  1310.    case GLSL_TYPE_INT:
  1311.    case GLSL_TYPE_FLOAT:
  1312.    case GLSL_TYPE_BOOL:
  1313.    case GLSL_TYPE_DOUBLE:
  1314.       return this->matrix_columns;
  1315.  
  1316.    case GLSL_TYPE_STRUCT:
  1317.    case GLSL_TYPE_INTERFACE: {
  1318.       unsigned size = 0;
  1319.  
  1320.       for (unsigned i = 0; i < this->length; i++)
  1321.          size += this->fields.structure[i].type->count_attribute_slots();
  1322.  
  1323.       return size;
  1324.    }
  1325.  
  1326.    case GLSL_TYPE_ARRAY:
  1327.       return this->length * this->fields.array->count_attribute_slots();
  1328.  
  1329.    case GLSL_TYPE_SAMPLER:
  1330.    case GLSL_TYPE_IMAGE:
  1331.    case GLSL_TYPE_ATOMIC_UINT:
  1332.    case GLSL_TYPE_VOID:
  1333.    case GLSL_TYPE_ERROR:
  1334.       break;
  1335.    }
  1336.  
  1337.    assert(!"Unexpected type in count_attribute_slots()");
  1338.  
  1339.    return 0;
  1340. }
  1341.  
  1342. int
  1343. glsl_type::coordinate_components() const
  1344. {
  1345.    int size;
  1346.  
  1347.    switch (sampler_dimensionality) {
  1348.    case GLSL_SAMPLER_DIM_1D:
  1349.    case GLSL_SAMPLER_DIM_BUF:
  1350.       size = 1;
  1351.       break;
  1352.    case GLSL_SAMPLER_DIM_2D:
  1353.    case GLSL_SAMPLER_DIM_RECT:
  1354.    case GLSL_SAMPLER_DIM_MS:
  1355.    case GLSL_SAMPLER_DIM_EXTERNAL:
  1356.       size = 2;
  1357.       break;
  1358.    case GLSL_SAMPLER_DIM_3D:
  1359.    case GLSL_SAMPLER_DIM_CUBE:
  1360.       size = 3;
  1361.       break;
  1362.    default:
  1363.       assert(!"Should not get here.");
  1364.       size = 1;
  1365.       break;
  1366.    }
  1367.  
  1368.    /* Array textures need an additional component for the array index, except
  1369.     * for cubemap array images that behave like a 2D array of interleaved
  1370.     * cubemap faces.
  1371.     */
  1372.    if (sampler_array &&
  1373.        !(base_type == GLSL_TYPE_IMAGE &&
  1374.          sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
  1375.       size += 1;
  1376.  
  1377.    return size;
  1378. }
  1379.