Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  5.  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a
  8.  * copy of this software and associated documentation files (the "Software"),
  9.  * to deal in the Software without restriction, including without limitation
  10.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11.  * and/or sell copies of the Software, and to permit persons to whom the
  12.  * Software is furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included
  15.  * in all copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  20.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23.  * OTHER DEALINGS IN THE SOFTWARE.
  24.  */
  25.  
  26.  
  27. #include <stdio.h>
  28. #include <inttypes.h>  /* for PRId64 macro */
  29.  
  30. #include "glheader.h"
  31. #include "imports.h"
  32. #include "bufferobj.h"
  33. #include "context.h"
  34. #include "enable.h"
  35. #include "enums.h"
  36. #include "hash.h"
  37. #include "image.h"
  38. #include "macros.h"
  39. #include "mtypes.h"
  40. #include "varray.h"
  41. #include "arrayobj.h"
  42. #include "main/dispatch.h"
  43.  
  44.  
  45. /** Used to do error checking for GL_EXT_vertex_array_bgra */
  46. #define BGRA_OR_4  5
  47.  
  48.  
  49. /** Used to indicate which GL datatypes are accepted by each of the
  50.  * glVertex/Color/Attrib/EtcPointer() functions.
  51.  */
  52. #define BOOL_BIT                          (1 << 0)
  53. #define BYTE_BIT                          (1 << 1)
  54. #define UNSIGNED_BYTE_BIT                 (1 << 2)
  55. #define SHORT_BIT                         (1 << 3)
  56. #define UNSIGNED_SHORT_BIT                (1 << 4)
  57. #define INT_BIT                           (1 << 5)
  58. #define UNSIGNED_INT_BIT                  (1 << 6)
  59. #define HALF_BIT                          (1 << 7)
  60. #define FLOAT_BIT                         (1 << 8)
  61. #define DOUBLE_BIT                        (1 << 9)
  62. #define FIXED_ES_BIT                      (1 << 10)
  63. #define FIXED_GL_BIT                      (1 << 11)
  64. #define UNSIGNED_INT_2_10_10_10_REV_BIT   (1 << 12)
  65. #define INT_2_10_10_10_REV_BIT            (1 << 13)
  66. #define UNSIGNED_INT_10F_11F_11F_REV_BIT  (1 << 14)
  67. #define ALL_TYPE_BITS                    ((1 << 15) - 1)
  68.  
  69. #define ATTRIB_FORMAT_TYPES_MASK (BYTE_BIT | UNSIGNED_BYTE_BIT | \
  70.                                   SHORT_BIT | UNSIGNED_SHORT_BIT | \
  71.                                   INT_BIT | UNSIGNED_INT_BIT | \
  72.                                   HALF_BIT | FLOAT_BIT | DOUBLE_BIT | \
  73.                                   FIXED_GL_BIT | \
  74.                                   UNSIGNED_INT_2_10_10_10_REV_BIT | \
  75.                                   INT_2_10_10_10_REV_BIT | \
  76.                                   UNSIGNED_INT_10F_11F_11F_REV_BIT)
  77.  
  78. #define ATTRIB_IFORMAT_TYPES_MASK (BYTE_BIT | UNSIGNED_BYTE_BIT | \
  79.                                    SHORT_BIT | UNSIGNED_SHORT_BIT | \
  80.                                    INT_BIT | UNSIGNED_INT_BIT)
  81.  
  82. #define ATTRIB_LFORMAT_TYPES_MASK DOUBLE_BIT
  83.  
  84.  
  85. /** Convert GL datatype enum into a <type>_BIT value seen above */
  86. static GLbitfield
  87. type_to_bit(const struct gl_context *ctx, GLenum type)
  88. {
  89.    switch (type) {
  90.    case GL_BOOL:
  91.       return BOOL_BIT;
  92.    case GL_BYTE:
  93.       return BYTE_BIT;
  94.    case GL_UNSIGNED_BYTE:
  95.       return UNSIGNED_BYTE_BIT;
  96.    case GL_SHORT:
  97.       return SHORT_BIT;
  98.    case GL_UNSIGNED_SHORT:
  99.       return UNSIGNED_SHORT_BIT;
  100.    case GL_INT:
  101.       return INT_BIT;
  102.    case GL_UNSIGNED_INT:
  103.       return UNSIGNED_INT_BIT;
  104.    case GL_HALF_FLOAT:
  105.       if (ctx->Extensions.ARB_half_float_vertex)
  106.          return HALF_BIT;
  107.       else
  108.          return 0x0;
  109.    case GL_FLOAT:
  110.       return FLOAT_BIT;
  111.    case GL_DOUBLE:
  112.       return DOUBLE_BIT;
  113.    case GL_FIXED:
  114.       return _mesa_is_desktop_gl(ctx) ? FIXED_GL_BIT : FIXED_ES_BIT;
  115.    case GL_UNSIGNED_INT_2_10_10_10_REV:
  116.       return UNSIGNED_INT_2_10_10_10_REV_BIT;
  117.    case GL_INT_2_10_10_10_REV:
  118.       return INT_2_10_10_10_REV_BIT;
  119.    case GL_UNSIGNED_INT_10F_11F_11F_REV:
  120.       return UNSIGNED_INT_10F_11F_11F_REV_BIT;
  121.    default:
  122.       return 0;
  123.    }
  124. }
  125.  
  126.  
  127. /**
  128.  * Sets the VertexBinding field in the vertex attribute given by attribIndex.
  129.  */
  130. static void
  131. vertex_attrib_binding(struct gl_context *ctx,
  132.                       struct gl_vertex_array_object *vao,
  133.                       GLuint attribIndex,
  134.                       GLuint bindingIndex)
  135. {
  136.    struct gl_vertex_attrib_array *array = &vao->VertexAttrib[attribIndex];
  137.  
  138.    if (array->VertexBinding != bindingIndex) {
  139.       const GLbitfield64 array_bit = VERT_BIT(attribIndex);
  140.  
  141.       FLUSH_VERTICES(ctx, _NEW_ARRAY);
  142.  
  143.       vao->VertexBinding[array->VertexBinding]._BoundArrays &= ~array_bit;
  144.       vao->VertexBinding[bindingIndex]._BoundArrays |= array_bit;
  145.  
  146.       array->VertexBinding = bindingIndex;
  147.  
  148.       vao->NewArrays |= array_bit;
  149.    }
  150. }
  151.  
  152.  
  153. /**
  154.  * Binds a buffer object to the vertex buffer binding point given by index,
  155.  * and sets the Offset and Stride fields.
  156.  */
  157. static void
  158. bind_vertex_buffer(struct gl_context *ctx,
  159.                    struct gl_vertex_array_object *vao,
  160.                    GLuint index,
  161.                    struct gl_buffer_object *vbo,
  162.                    GLintptr offset, GLsizei stride)
  163. {
  164.    struct gl_vertex_buffer_binding *binding = &vao->VertexBinding[index];
  165.  
  166.    if (binding->BufferObj != vbo ||
  167.        binding->Offset != offset ||
  168.        binding->Stride != stride) {
  169.  
  170.       FLUSH_VERTICES(ctx, _NEW_ARRAY);
  171.  
  172.       _mesa_reference_buffer_object(ctx, &binding->BufferObj, vbo);
  173.  
  174.       binding->Offset = offset;
  175.       binding->Stride = stride;
  176.  
  177.       vao->NewArrays |= binding->_BoundArrays;
  178.    }
  179. }
  180.  
  181.  
  182. /**
  183.  * Sets the InstanceDivisor field in the vertex buffer binding point
  184.  * given by bindingIndex.
  185.  */
  186. static void
  187. vertex_binding_divisor(struct gl_context *ctx,
  188.                        struct gl_vertex_array_object *vao,
  189.                        GLuint bindingIndex,
  190.                        GLuint divisor)
  191. {
  192.    struct gl_vertex_buffer_binding *binding =
  193.       &vao->VertexBinding[bindingIndex];
  194.  
  195.    if (binding->InstanceDivisor != divisor) {
  196.       FLUSH_VERTICES(ctx, _NEW_ARRAY);
  197.       binding->InstanceDivisor = divisor;
  198.       vao->NewArrays |= binding->_BoundArrays;
  199.    }
  200. }
  201.  
  202.  
  203. /**
  204.  * Examine the API profile and extensions to determine which types are legal
  205.  * for vertex arrays.  This is called once from update_array_format().
  206.  */
  207. static GLbitfield
  208. get_legal_types_mask(const struct gl_context *ctx)
  209. {
  210.    GLbitfield legalTypesMask = ALL_TYPE_BITS;
  211.  
  212.    if (_mesa_is_gles(ctx)) {
  213.       legalTypesMask &= ~(FIXED_GL_BIT |
  214.                           DOUBLE_BIT |
  215.                           UNSIGNED_INT_10F_11F_11F_REV_BIT);
  216.  
  217.       /* GL_INT and GL_UNSIGNED_INT data is not allowed in OpenGL ES until
  218.        * 3.0.  The 2_10_10_10 types are added in OpenGL ES 3.0 or
  219.        * GL_OES_vertex_type_10_10_10_2.  GL_HALF_FLOAT data is not allowed
  220.        * until 3.0 or with the GL_OES_vertex_half float extension, which isn't
  221.        * quite as trivial as we'd like because it uses a different enum value
  222.        * for GL_HALF_FLOAT_OES.
  223.        */
  224.       if (ctx->Version < 30) {
  225.          legalTypesMask &= ~(UNSIGNED_INT_BIT |
  226.                              INT_BIT |
  227.                              UNSIGNED_INT_2_10_10_10_REV_BIT |
  228.                              INT_2_10_10_10_REV_BIT |
  229.                              HALF_BIT);
  230.       }
  231.    }
  232.    else {
  233.       legalTypesMask &= ~FIXED_ES_BIT;
  234.  
  235.       if (!ctx->Extensions.ARB_ES2_compatibility)
  236.          legalTypesMask &= ~FIXED_GL_BIT;
  237.  
  238.       if (!ctx->Extensions.ARB_vertex_type_2_10_10_10_rev)
  239.          legalTypesMask &= ~(UNSIGNED_INT_2_10_10_10_REV_BIT |
  240.                              INT_2_10_10_10_REV_BIT);
  241.  
  242.       if (!ctx->Extensions.ARB_vertex_type_10f_11f_11f_rev)
  243.          legalTypesMask &= ~UNSIGNED_INT_10F_11F_11F_REV_BIT;
  244.    }
  245.  
  246.    return legalTypesMask;
  247. }
  248.  
  249.  
  250. /**
  251.  * Does error checking and updates the format in an attrib array.
  252.  *
  253.  * Called by update_array() and VertexAttrib*Format().
  254.  *
  255.  * \param func         Name of calling function used for error reporting
  256.  * \param attrib       The index of the attribute array
  257.  * \param legalTypes   Bitmask of *_BIT above indicating legal datatypes
  258.  * \param sizeMin      Min allowable size value
  259.  * \param sizeMax      Max allowable size value (may also be BGRA_OR_4)
  260.  * \param size         Components per element (1, 2, 3 or 4)
  261.  * \param type         Datatype of each component (GL_FLOAT, GL_INT, etc)
  262.  * \param normalized   Whether integer types are converted to floats in [-1, 1]
  263.  * \param integer      Integer-valued values (will not be normalized to [-1, 1])
  264.  * \param doubles      Double values not reduced to floats
  265.  * \param relativeOffset Offset of the first element relative to the binding offset.
  266.  */
  267. static bool
  268. update_array_format(struct gl_context *ctx,
  269.                     const char *func,
  270.                     struct gl_vertex_array_object *vao,
  271.                     GLuint attrib, GLbitfield legalTypesMask,
  272.                     GLint sizeMin, GLint sizeMax,
  273.                     GLint size, GLenum type,
  274.                     GLboolean normalized, GLboolean integer, GLboolean doubles,
  275.                     GLuint relativeOffset)
  276. {
  277.    struct gl_vertex_attrib_array *array;
  278.    GLbitfield typeBit;
  279.    GLint elementSize;
  280.    GLenum format = GL_RGBA;
  281.  
  282.    if (ctx->Array.LegalTypesMask == 0 || ctx->Array.LegalTypesMaskAPI != ctx->API) {
  283.       /* Compute the LegalTypesMask only once, unless the context API has
  284.        * changed, in which case we want to compute it again.  We can't do this
  285.        * in _mesa_init_varrays() below because extensions are not yet enabled
  286.        * at that point.
  287.        */
  288.       ctx->Array.LegalTypesMask = get_legal_types_mask(ctx);
  289.       ctx->Array.LegalTypesMaskAPI = ctx->API;
  290.    }
  291.  
  292.    legalTypesMask &= ctx->Array.LegalTypesMask;
  293.  
  294.    if (_mesa_is_gles(ctx) && sizeMax == BGRA_OR_4) {
  295.       /* BGRA ordering is not supported in ES contexts.
  296.        */
  297.       sizeMax = 4;
  298.    }
  299.  
  300.    typeBit = type_to_bit(ctx, type);
  301.    if (typeBit == 0x0 || (typeBit & legalTypesMask) == 0x0) {
  302.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(type = %s)",
  303.                   func, _mesa_lookup_enum_by_nr(type));
  304.       return false;
  305.    }
  306.  
  307.    /* Do size parameter checking.
  308.     * If sizeMax = BGRA_OR_4 it means that size = GL_BGRA is legal and
  309.     * must be handled specially.
  310.     */
  311.    if (ctx->Extensions.EXT_vertex_array_bgra &&
  312.        sizeMax == BGRA_OR_4 &&
  313.        size == GL_BGRA) {
  314.       /* Page 298 of the PDF of the OpenGL 4.3 (Core Profile) spec says:
  315.        *
  316.        * "An INVALID_OPERATION error is generated under any of the following
  317.        *  conditions:
  318.        *    ...
  319.        *    â€¢ size is BGRA and type is not UNSIGNED_BYTE, INT_2_10_10_10_REV
  320.        *      or UNSIGNED_INT_2_10_10_10_REV;
  321.        *    ...
  322.        *    â€¢ size is BGRA and normalized is FALSE;"
  323.        */
  324.       bool bgra_error = false;
  325.  
  326.       if (ctx->Extensions.ARB_vertex_type_2_10_10_10_rev) {
  327.          if (type != GL_UNSIGNED_INT_2_10_10_10_REV &&
  328.              type != GL_INT_2_10_10_10_REV &&
  329.              type != GL_UNSIGNED_BYTE)
  330.             bgra_error = true;
  331.       } else if (type != GL_UNSIGNED_BYTE)
  332.          bgra_error = true;
  333.  
  334.       if (bgra_error) {
  335.          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(size=GL_BGRA and type=%s)",
  336.                      func, _mesa_lookup_enum_by_nr(type));
  337.          return false;
  338.       }
  339.  
  340.       if (!normalized) {
  341.          _mesa_error(ctx, GL_INVALID_OPERATION,
  342.                      "%s(size=GL_BGRA and normalized=GL_FALSE)", func);
  343.          return false;
  344.       }
  345.  
  346.       format = GL_BGRA;
  347.       size = 4;
  348.    }
  349.    else if (size < sizeMin || size > sizeMax || size > 4) {
  350.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d)", func, size);
  351.       return false;
  352.    }
  353.  
  354.    if (ctx->Extensions.ARB_vertex_type_2_10_10_10_rev &&
  355.        (type == GL_UNSIGNED_INT_2_10_10_10_REV ||
  356.         type == GL_INT_2_10_10_10_REV) && size != 4) {
  357.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(size=%d)", func, size);
  358.       return false;
  359.    }
  360.  
  361.    /* The ARB_vertex_attrib_binding_spec says:
  362.     *
  363.     *   An INVALID_VALUE error is generated if <relativeoffset> is larger than
  364.     *   the value of MAX_VERTEX_ATTRIB_RELATIVE_OFFSET.
  365.     */
  366.    if (relativeOffset > ctx->Const.MaxVertexAttribRelativeOffset) {
  367.       _mesa_error(ctx, GL_INVALID_VALUE,
  368.                   "%s(relativeOffset=%d > "
  369.                   "GL_MAX_VERTEX_ATTRIB_RELATIVE_OFFSET)",
  370.                   func, relativeOffset);
  371.       return false;
  372.    }
  373.  
  374.    if (ctx->Extensions.ARB_vertex_type_10f_11f_11f_rev &&
  375.          type == GL_UNSIGNED_INT_10F_11F_11F_REV && size != 3) {
  376.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(size=%d)", func, size);
  377.       return false;
  378.    }
  379.  
  380.    assert(size <= 4);
  381.  
  382.    elementSize = _mesa_bytes_per_vertex_attrib(size, type);
  383.    assert(elementSize != -1);
  384.  
  385.    array = &vao->VertexAttrib[attrib];
  386.    array->Size = size;
  387.    array->Type = type;
  388.    array->Format = format;
  389.    array->Normalized = normalized;
  390.    array->Integer = integer;
  391.    array->Doubles = doubles;
  392.    array->RelativeOffset = relativeOffset;
  393.    array->_ElementSize = elementSize;
  394.  
  395.    vao->NewArrays |= VERT_BIT(attrib);
  396.    ctx->NewState |= _NEW_ARRAY;
  397.  
  398.    return true;
  399. }
  400.  
  401.  
  402. /**
  403.  * Do error checking and update state for glVertex/Color/TexCoord/...Pointer
  404.  * functions.
  405.  *
  406.  * \param func  name of calling function used for error reporting
  407.  * \param attrib  the attribute array index to update
  408.  * \param legalTypes  bitmask of *_BIT above indicating legal datatypes
  409.  * \param sizeMin  min allowable size value
  410.  * \param sizeMax  max allowable size value (may also be BGRA_OR_4)
  411.  * \param size  components per element (1, 2, 3 or 4)
  412.  * \param type  datatype of each component (GL_FLOAT, GL_INT, etc)
  413.  * \param stride  stride between elements, in elements
  414.  * \param normalized  are integer types converted to floats in [-1, 1]?
  415.  * \param integer  integer-valued values (will not be normalized to [-1,1])
  416.  * \param doubles  Double values not reduced to floats
  417.  * \param ptr  the address (or offset inside VBO) of the array data
  418.  */
  419. static void
  420. update_array(struct gl_context *ctx,
  421.              const char *func,
  422.              GLuint attrib, GLbitfield legalTypesMask,
  423.              GLint sizeMin, GLint sizeMax,
  424.              GLint size, GLenum type, GLsizei stride,
  425.              GLboolean normalized, GLboolean integer, GLboolean doubles,
  426.              const GLvoid *ptr)
  427. {
  428.    struct gl_vertex_attrib_array *array;
  429.    GLsizei effectiveStride;
  430.  
  431.    /* Page 407 (page 423 of the PDF) of the OpenGL 3.0 spec says:
  432.     *
  433.     *     "Client vertex arrays - all vertex array attribute pointers must
  434.     *     refer to buffer objects (section 2.9.2). The default vertex array
  435.     *     object (the name zero) is also deprecated. Calling
  436.     *     VertexAttribPointer when no buffer object or no vertex array object
  437.     *     is bound will generate an INVALID_OPERATION error..."
  438.     *
  439.     * The check for VBOs is handled below.
  440.     */
  441.    if (ctx->API == API_OPENGL_CORE
  442.        && (ctx->Array.VAO == ctx->Array.DefaultVAO)) {
  443.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no array object bound)",
  444.                   func);
  445.       return;
  446.    }
  447.  
  448.    if (stride < 0) {
  449.       _mesa_error( ctx, GL_INVALID_VALUE, "%s(stride=%d)", func, stride );
  450.       return;
  451.    }
  452.  
  453.    if (ctx->API == API_OPENGL_CORE && ctx->Version >= 44 &&
  454.        stride > ctx->Const.MaxVertexAttribStride) {
  455.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(stride=%d > "
  456.                   "GL_MAX_VERTEX_ATTRIB_STRIDE)", func, stride);
  457.       return;
  458.    }
  459.  
  460.    /* Page 29 (page 44 of the PDF) of the OpenGL 3.3 spec says:
  461.     *
  462.     *     "An INVALID_OPERATION error is generated under any of the following
  463.     *     conditions:
  464.     *
  465.     *     ...
  466.     *
  467.     *     * any of the *Pointer commands specifying the location and
  468.     *       organization of vertex array data are called while zero is bound
  469.     *       to the ARRAY_BUFFER buffer object binding point (see section
  470.     *       2.9.6), and the pointer argument is not NULL."
  471.     */
  472.    if (ptr != NULL && ctx->Array.VAO->ARBsemantics &&
  473.        !_mesa_is_bufferobj(ctx->Array.ArrayBufferObj)) {
  474.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-VBO array)", func);
  475.       return;
  476.    }
  477.  
  478.    if (!update_array_format(ctx, func, ctx->Array.VAO, attrib,
  479.                             legalTypesMask, sizeMin, sizeMax,
  480.                             size, type, normalized, integer, doubles, 0)) {
  481.       return;
  482.    }
  483.  
  484.    /* Reset the vertex attrib binding */
  485.    vertex_attrib_binding(ctx, ctx->Array.VAO, attrib, attrib);
  486.  
  487.    /* The Stride and Ptr fields are not set by update_array_format() */
  488.    array = &ctx->Array.VAO->VertexAttrib[attrib];
  489.    array->Stride = stride;
  490.    array->Ptr = (const GLvoid *) ptr;
  491.  
  492.    /* Update the vertex buffer binding */
  493.    effectiveStride = stride != 0 ? stride : array->_ElementSize;
  494.    bind_vertex_buffer(ctx, ctx->Array.VAO, attrib, ctx->Array.ArrayBufferObj,
  495.                       (GLintptr) ptr, effectiveStride);
  496. }
  497.  
  498.  
  499. void GLAPIENTRY
  500. _mesa_VertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr)
  501. {
  502.    GET_CURRENT_CONTEXT(ctx);
  503.    GLbitfield legalTypes = (ctx->API == API_OPENGLES)
  504.       ? (BYTE_BIT | SHORT_BIT | FLOAT_BIT | FIXED_ES_BIT)
  505.       : (SHORT_BIT | INT_BIT | FLOAT_BIT |
  506.          DOUBLE_BIT | HALF_BIT |
  507.          UNSIGNED_INT_2_10_10_10_REV_BIT |
  508.          INT_2_10_10_10_REV_BIT);
  509.  
  510.    FLUSH_VERTICES(ctx, 0);
  511.  
  512.    update_array(ctx, "glVertexPointer", VERT_ATTRIB_POS,
  513.                 legalTypes, 2, 4,
  514.                 size, type, stride, GL_FALSE, GL_FALSE, GL_FALSE, ptr);
  515. }
  516.  
  517.  
  518. void GLAPIENTRY
  519. _mesa_NormalPointer(GLenum type, GLsizei stride, const GLvoid *ptr )
  520. {
  521.    GET_CURRENT_CONTEXT(ctx);
  522.    const GLbitfield legalTypes = (ctx->API == API_OPENGLES)
  523.       ? (BYTE_BIT | SHORT_BIT | FLOAT_BIT | FIXED_ES_BIT)
  524.       : (BYTE_BIT | SHORT_BIT | INT_BIT |
  525.          HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
  526.          UNSIGNED_INT_2_10_10_10_REV_BIT |
  527.          INT_2_10_10_10_REV_BIT);
  528.  
  529.    FLUSH_VERTICES(ctx, 0);
  530.  
  531.    update_array(ctx, "glNormalPointer", VERT_ATTRIB_NORMAL,
  532.                 legalTypes, 3, 3,
  533.                 3, type, stride, GL_TRUE, GL_FALSE, GL_FALSE, ptr);
  534. }
  535.  
  536.  
  537. void GLAPIENTRY
  538. _mesa_ColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr)
  539. {
  540.    GET_CURRENT_CONTEXT(ctx);
  541.    const GLbitfield legalTypes = (ctx->API == API_OPENGLES)
  542.       ? (UNSIGNED_BYTE_BIT | HALF_BIT | FLOAT_BIT | FIXED_ES_BIT)
  543.       : (BYTE_BIT | UNSIGNED_BYTE_BIT |
  544.          SHORT_BIT | UNSIGNED_SHORT_BIT |
  545.          INT_BIT | UNSIGNED_INT_BIT |
  546.          HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
  547.          UNSIGNED_INT_2_10_10_10_REV_BIT |
  548.          INT_2_10_10_10_REV_BIT);
  549.    const GLint sizeMin = (ctx->API == API_OPENGLES) ? 4 : 3;
  550.  
  551.    FLUSH_VERTICES(ctx, 0);
  552.  
  553.    update_array(ctx, "glColorPointer", VERT_ATTRIB_COLOR0,
  554.                 legalTypes, sizeMin, BGRA_OR_4,
  555.                 size, type, stride, GL_TRUE, GL_FALSE, GL_FALSE, ptr);
  556. }
  557.  
  558.  
  559. void GLAPIENTRY
  560. _mesa_FogCoordPointer(GLenum type, GLsizei stride, const GLvoid *ptr)
  561. {
  562.    const GLbitfield legalTypes = (HALF_BIT | FLOAT_BIT | DOUBLE_BIT);
  563.    GET_CURRENT_CONTEXT(ctx);
  564.  
  565.    FLUSH_VERTICES(ctx, 0);
  566.  
  567.    update_array(ctx, "glFogCoordPointer", VERT_ATTRIB_FOG,
  568.                 legalTypes, 1, 1,
  569.                 1, type, stride, GL_FALSE, GL_FALSE, GL_FALSE, ptr);
  570. }
  571.  
  572.  
  573. void GLAPIENTRY
  574. _mesa_IndexPointer(GLenum type, GLsizei stride, const GLvoid *ptr)
  575. {
  576.    const GLbitfield legalTypes = (UNSIGNED_BYTE_BIT | SHORT_BIT | INT_BIT |
  577.                                   FLOAT_BIT | DOUBLE_BIT);
  578.    GET_CURRENT_CONTEXT(ctx);
  579.  
  580.    FLUSH_VERTICES(ctx, 0);
  581.  
  582.    update_array(ctx, "glIndexPointer", VERT_ATTRIB_COLOR_INDEX,
  583.                 legalTypes, 1, 1,
  584.                 1, type, stride, GL_FALSE, GL_FALSE, GL_FALSE, ptr);
  585. }
  586.  
  587.  
  588. void GLAPIENTRY
  589. _mesa_SecondaryColorPointer(GLint size, GLenum type,
  590.                                GLsizei stride, const GLvoid *ptr)
  591. {
  592.    const GLbitfield legalTypes = (BYTE_BIT | UNSIGNED_BYTE_BIT |
  593.                                   SHORT_BIT | UNSIGNED_SHORT_BIT |
  594.                                   INT_BIT | UNSIGNED_INT_BIT |
  595.                                   HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
  596.                                   UNSIGNED_INT_2_10_10_10_REV_BIT |
  597.                                   INT_2_10_10_10_REV_BIT);
  598.    GET_CURRENT_CONTEXT(ctx);
  599.  
  600.    FLUSH_VERTICES(ctx, 0);
  601.  
  602.    update_array(ctx, "glSecondaryColorPointer", VERT_ATTRIB_COLOR1,
  603.                 legalTypes, 3, BGRA_OR_4,
  604.                 size, type, stride, GL_TRUE, GL_FALSE, GL_FALSE, ptr);
  605. }
  606.  
  607.  
  608. void GLAPIENTRY
  609. _mesa_TexCoordPointer(GLint size, GLenum type, GLsizei stride,
  610.                       const GLvoid *ptr)
  611. {
  612.    GET_CURRENT_CONTEXT(ctx);
  613.    GLbitfield legalTypes = (ctx->API == API_OPENGLES)
  614.       ? (BYTE_BIT | SHORT_BIT | FLOAT_BIT | FIXED_ES_BIT)
  615.       : (SHORT_BIT | INT_BIT |
  616.          HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
  617.          UNSIGNED_INT_2_10_10_10_REV_BIT |
  618.          INT_2_10_10_10_REV_BIT);
  619.    const GLint sizeMin = (ctx->API == API_OPENGLES) ? 2 : 1;
  620.    const GLuint unit = ctx->Array.ActiveTexture;
  621.  
  622.    FLUSH_VERTICES(ctx, 0);
  623.  
  624.    update_array(ctx, "glTexCoordPointer", VERT_ATTRIB_TEX(unit),
  625.                 legalTypes, sizeMin, 4,
  626.                 size, type, stride, GL_FALSE, GL_FALSE, GL_FALSE,
  627.                 ptr);
  628. }
  629.  
  630.  
  631. void GLAPIENTRY
  632. _mesa_EdgeFlagPointer(GLsizei stride, const GLvoid *ptr)
  633. {
  634.    const GLbitfield legalTypes = UNSIGNED_BYTE_BIT;
  635.    /* this is the same type that glEdgeFlag uses */
  636.    const GLboolean integer = GL_FALSE;
  637.    GET_CURRENT_CONTEXT(ctx);
  638.  
  639.    FLUSH_VERTICES(ctx, 0);
  640.  
  641.    update_array(ctx, "glEdgeFlagPointer", VERT_ATTRIB_EDGEFLAG,
  642.                 legalTypes, 1, 1,
  643.                 1, GL_UNSIGNED_BYTE, stride, GL_FALSE, integer, GL_FALSE, ptr);
  644. }
  645.  
  646.  
  647. void GLAPIENTRY
  648. _mesa_PointSizePointerOES(GLenum type, GLsizei stride, const GLvoid *ptr)
  649. {
  650.    const GLbitfield legalTypes = (FLOAT_BIT | FIXED_ES_BIT);
  651.    GET_CURRENT_CONTEXT(ctx);
  652.  
  653.    FLUSH_VERTICES(ctx, 0);
  654.  
  655.    if (ctx->API != API_OPENGLES) {
  656.       _mesa_error(ctx, GL_INVALID_OPERATION,
  657.                   "glPointSizePointer(ES 1.x only)");
  658.       return;
  659.    }
  660.      
  661.    update_array(ctx, "glPointSizePointer", VERT_ATTRIB_POINT_SIZE,
  662.                 legalTypes, 1, 1,
  663.                 1, type, stride, GL_FALSE, GL_FALSE, GL_FALSE, ptr);
  664. }
  665.  
  666.  
  667. /**
  668.  * Set a generic vertex attribute array.
  669.  * Note that these arrays DO NOT alias the conventional GL vertex arrays
  670.  * (position, normal, color, fog, texcoord, etc).
  671.  */
  672. void GLAPIENTRY
  673. _mesa_VertexAttribPointer(GLuint index, GLint size, GLenum type,
  674.                              GLboolean normalized,
  675.                              GLsizei stride, const GLvoid *ptr)
  676. {
  677.    const GLbitfield legalTypes = (BYTE_BIT | UNSIGNED_BYTE_BIT |
  678.                                   SHORT_BIT | UNSIGNED_SHORT_BIT |
  679.                                   INT_BIT | UNSIGNED_INT_BIT |
  680.                                   HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
  681.                                   FIXED_ES_BIT | FIXED_GL_BIT |
  682.                                   UNSIGNED_INT_2_10_10_10_REV_BIT |
  683.                                   INT_2_10_10_10_REV_BIT |
  684.                                   UNSIGNED_INT_10F_11F_11F_REV_BIT);
  685.    GET_CURRENT_CONTEXT(ctx);
  686.  
  687.    if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
  688.       _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribPointerARB(index)");
  689.       return;
  690.    }
  691.  
  692.    update_array(ctx, "glVertexAttribPointer", VERT_ATTRIB_GENERIC(index),
  693.                 legalTypes, 1, BGRA_OR_4,
  694.                 size, type, stride, normalized, GL_FALSE, GL_FALSE, ptr);
  695. }
  696.  
  697.  
  698. /**
  699.  * GL_EXT_gpu_shader4 / GL 3.0.
  700.  * Set an integer-valued vertex attribute array.
  701.  * Note that these arrays DO NOT alias the conventional GL vertex arrays
  702.  * (position, normal, color, fog, texcoord, etc).
  703.  */
  704. void GLAPIENTRY
  705. _mesa_VertexAttribIPointer(GLuint index, GLint size, GLenum type,
  706.                            GLsizei stride, const GLvoid *ptr)
  707. {
  708.    const GLbitfield legalTypes = (BYTE_BIT | UNSIGNED_BYTE_BIT |
  709.                                   SHORT_BIT | UNSIGNED_SHORT_BIT |
  710.                                   INT_BIT | UNSIGNED_INT_BIT);
  711.    const GLboolean normalized = GL_FALSE;
  712.    const GLboolean integer = GL_TRUE;
  713.    GET_CURRENT_CONTEXT(ctx);
  714.  
  715.    if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
  716.       _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribIPointer(index)");
  717.       return;
  718.    }
  719.  
  720.    update_array(ctx, "glVertexAttribIPointer", VERT_ATTRIB_GENERIC(index),
  721.                 legalTypes, 1, 4,
  722.                 size, type, stride, normalized, integer, GL_FALSE, ptr);
  723. }
  724.  
  725. void GLAPIENTRY
  726. _mesa_VertexAttribLPointer(GLuint index, GLint size, GLenum type,
  727.                            GLsizei stride, const GLvoid *ptr)
  728. {
  729.    GET_CURRENT_CONTEXT(ctx);
  730.    const GLbitfield legalTypes = (DOUBLE_BIT);
  731.    if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
  732.       _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribLPointer(index)");
  733.       return;
  734.    }
  735.  
  736.    update_array(ctx, "glVertexAttribLPointer", VERT_ATTRIB_GENERIC(index),
  737.                 legalTypes, 1, 4,
  738.                 size, type, stride, GL_TRUE, GL_FALSE, GL_TRUE, ptr);
  739. }
  740.  
  741.  
  742. static void
  743. enable_vertex_array_attrib(struct gl_context *ctx,
  744.                            struct gl_vertex_array_object *vao,
  745.                            GLuint index,
  746.                            const char *func)
  747. {
  748.    if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
  749.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(index)", func);
  750.       return;
  751.    }
  752.  
  753.    assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(vao->VertexAttrib));
  754.  
  755.    if (!vao->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled) {
  756.       /* was disabled, now being enabled */
  757.       FLUSH_VERTICES(ctx, _NEW_ARRAY);
  758.       vao->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled = GL_TRUE;
  759.       vao->_Enabled |= VERT_BIT_GENERIC(index);
  760.       vao->NewArrays |= VERT_BIT_GENERIC(index);
  761.    }
  762. }
  763.  
  764.  
  765. void GLAPIENTRY
  766. _mesa_EnableVertexAttribArray(GLuint index)
  767. {
  768.    GET_CURRENT_CONTEXT(ctx);
  769.    enable_vertex_array_attrib(ctx, ctx->Array.VAO, index,
  770.                               "glEnableVertexAttribArray");
  771. }
  772.  
  773.  
  774. void GLAPIENTRY
  775. _mesa_EnableVertexArrayAttrib(GLuint vaobj, GLuint index)
  776. {
  777.    GET_CURRENT_CONTEXT(ctx);
  778.    struct gl_vertex_array_object *vao;
  779.  
  780.    if (!ctx->Extensions.ARB_direct_state_access) {
  781.       _mesa_error(ctx, GL_INVALID_OPERATION,
  782.                    "glEnableVertexArrayAttrib(GL_ARB_direct_state_access "
  783.                    "is not supported");
  784.       return;
  785.    }
  786.  
  787.    /* The ARB_direct_state_access specification says:
  788.     *
  789.     *   "An INVALID_OPERATION error is generated by EnableVertexArrayAttrib
  790.     *    and DisableVertexArrayAttrib if <vaobj> is not
  791.     *    [compatibility profile: zero or] the name of an existing vertex
  792.     *    array object."
  793.     */
  794.    vao = _mesa_lookup_vao_err(ctx, vaobj, "glEnableVertexArrayAttrib");
  795.    if (!vao)
  796.       return;
  797.  
  798.    enable_vertex_array_attrib(ctx, vao, index, "glEnableVertexArrayAttrib");
  799. }
  800.  
  801.  
  802. static void
  803. disable_vertex_array_attrib(struct gl_context *ctx,
  804.                             struct gl_vertex_array_object *vao,
  805.                             GLuint index,
  806.                             const char *func)
  807. {
  808.    if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
  809.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(index)", func);
  810.       return;
  811.    }
  812.  
  813.    assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(vao->VertexAttrib));
  814.  
  815.    if (vao->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled) {
  816.       /* was enabled, now being disabled */
  817.       FLUSH_VERTICES(ctx, _NEW_ARRAY);
  818.       vao->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled = GL_FALSE;
  819.       vao->_Enabled &= ~VERT_BIT_GENERIC(index);
  820.       vao->NewArrays |= VERT_BIT_GENERIC(index);
  821.    }
  822. }
  823.  
  824.  
  825. void GLAPIENTRY
  826. _mesa_DisableVertexAttribArray(GLuint index)
  827. {
  828.    GET_CURRENT_CONTEXT(ctx);
  829.    disable_vertex_array_attrib(ctx, ctx->Array.VAO, index,
  830.                                "glDisableVertexAttribArray");
  831. }
  832.  
  833.  
  834. void GLAPIENTRY
  835. _mesa_DisableVertexArrayAttrib(GLuint vaobj, GLuint index)
  836. {
  837.    GET_CURRENT_CONTEXT(ctx);
  838.    struct gl_vertex_array_object *vao;
  839.  
  840.    if (!ctx->Extensions.ARB_direct_state_access) {
  841.       _mesa_error(ctx, GL_INVALID_OPERATION,
  842.                    "glDisableVertexArrayAttrib(GL_ARB_direct_state_access "
  843.                    "is not supported");
  844.       return;
  845.    }
  846.  
  847.    /* The ARB_direct_state_access specification says:
  848.     *
  849.     *   "An INVALID_OPERATION error is generated by EnableVertexArrayAttrib
  850.     *    and DisableVertexArrayAttrib if <vaobj> is not
  851.     *    [compatibility profile: zero or] the name of an existing vertex
  852.     *    array object."
  853.     */
  854.    vao = _mesa_lookup_vao_err(ctx, vaobj, "glDisableVertexArrayAttrib");
  855.    if (!vao)
  856.       return;
  857.  
  858.    disable_vertex_array_attrib(ctx, vao, index, "glDisableVertexArrayAttrib");
  859. }
  860.  
  861.  
  862. /**
  863.  * Return info for a vertex attribute array (no alias with legacy
  864.  * vertex attributes (pos, normal, color, etc)).  This function does
  865.  * not handle the 4-element GL_CURRENT_VERTEX_ATTRIB_ARB query.
  866.  */
  867. static GLuint
  868. get_vertex_array_attrib(struct gl_context *ctx,
  869.                         const struct gl_vertex_array_object *vao,
  870.                         GLuint index, GLenum pname,
  871.                         const char *caller)
  872. {
  873.    const struct gl_vertex_attrib_array *array;
  874.  
  875.    if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
  876.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%u)", caller, index);
  877.       return 0;
  878.    }
  879.  
  880.    assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(vao->VertexAttrib));
  881.  
  882.    array = &vao->VertexAttrib[VERT_ATTRIB_GENERIC(index)];
  883.  
  884.    switch (pname) {
  885.    case GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB:
  886.       return array->Enabled;
  887.    case GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB:
  888.       return (array->Format == GL_BGRA) ? GL_BGRA : array->Size;
  889.    case GL_VERTEX_ATTRIB_ARRAY_STRIDE_ARB:
  890.       return array->Stride;
  891.    case GL_VERTEX_ATTRIB_ARRAY_TYPE_ARB:
  892.       return array->Type;
  893.    case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED_ARB:
  894.       return array->Normalized;
  895.    case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB:
  896.       return vao->VertexBinding[array->VertexBinding].BufferObj->Name;
  897.    case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
  898.       if ((_mesa_is_desktop_gl(ctx)
  899.            && (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4))
  900.           || _mesa_is_gles3(ctx)) {
  901.          return array->Integer;
  902.       }
  903.       goto error;
  904.    case GL_VERTEX_ATTRIB_ARRAY_LONG:
  905.       if (_mesa_is_desktop_gl(ctx)) {
  906.          return array->Doubles;
  907.       }
  908.       goto error;
  909.    case GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ARB:
  910.       if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_instanced_arrays)
  911.           || _mesa_is_gles3(ctx)) {
  912.          return vao->VertexBinding[array->VertexBinding].InstanceDivisor;
  913.       }
  914.       goto error;
  915.    case GL_VERTEX_ATTRIB_BINDING:
  916.       if (_mesa_is_desktop_gl(ctx)) {
  917.          return array->VertexBinding - VERT_ATTRIB_GENERIC0;
  918.       }
  919.       goto error;
  920.    case GL_VERTEX_ATTRIB_RELATIVE_OFFSET:
  921.       if (_mesa_is_desktop_gl(ctx)) {
  922.          return array->RelativeOffset;
  923.       }
  924.       goto error;
  925.    default:
  926.       ; /* fall-through */
  927.    }
  928.  
  929. error:
  930.    _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", caller, pname);
  931.    return 0;
  932. }
  933.  
  934.  
  935. static const GLfloat *
  936. get_current_attrib(struct gl_context *ctx, GLuint index, const char *function)
  937. {
  938.    if (index == 0) {
  939.       if (_mesa_attr_zero_aliases_vertex(ctx)) {
  940.          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(index==0)", function);
  941.          return NULL;
  942.       }
  943.    }
  944.    else if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
  945.       _mesa_error(ctx, GL_INVALID_VALUE,
  946.                   "%s(index>=GL_MAX_VERTEX_ATTRIBS)", function);
  947.       return NULL;
  948.    }
  949.  
  950.    assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(ctx->Array.VAO->VertexAttrib));
  951.  
  952.    FLUSH_CURRENT(ctx, 0);
  953.    return ctx->Current.Attrib[VERT_ATTRIB_GENERIC(index)];
  954. }
  955.  
  956. void GLAPIENTRY
  957. _mesa_GetVertexAttribfv(GLuint index, GLenum pname, GLfloat *params)
  958. {
  959.    GET_CURRENT_CONTEXT(ctx);
  960.  
  961.    if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
  962.       const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribfv");
  963.       if (v != NULL) {
  964.          COPY_4V(params, v);
  965.       }
  966.    }
  967.    else {
  968.       params[0] = (GLfloat) get_vertex_array_attrib(ctx, ctx->Array.VAO,
  969.                                                     index, pname,
  970.                                                     "glGetVertexAttribfv");
  971.    }
  972. }
  973.  
  974.  
  975. void GLAPIENTRY
  976. _mesa_GetVertexAttribdv(GLuint index, GLenum pname, GLdouble *params)
  977. {
  978.    GET_CURRENT_CONTEXT(ctx);
  979.  
  980.    if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
  981.       const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribdv");
  982.       if (v != NULL) {
  983.          params[0] = (GLdouble) v[0];
  984.          params[1] = (GLdouble) v[1];
  985.          params[2] = (GLdouble) v[2];
  986.          params[3] = (GLdouble) v[3];
  987.       }
  988.    }
  989.    else {
  990.       params[0] = (GLdouble) get_vertex_array_attrib(ctx, ctx->Array.VAO,
  991.                                                      index, pname,
  992.                                                      "glGetVertexAttribdv");
  993.    }
  994. }
  995.  
  996. void GLAPIENTRY
  997. _mesa_GetVertexAttribLdv(GLuint index, GLenum pname, GLdouble *params)
  998. {
  999.    GET_CURRENT_CONTEXT(ctx);
  1000.  
  1001.    if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
  1002.       const GLdouble *v = (const GLdouble *)get_current_attrib(ctx, index, "glGetVertexAttribLdv");
  1003.       if (v != NULL) {
  1004.          params[0] = v[0];
  1005.          params[1] = v[1];
  1006.          params[2] = v[2];
  1007.          params[3] = v[3];
  1008.       }
  1009.    }
  1010.    else {
  1011.       params[0] = (GLdouble) get_vertex_array_attrib(ctx, ctx->Array.VAO,
  1012.                                                      index, pname,
  1013.                                                      "glGetVertexAttribLdv");
  1014.    }
  1015. }
  1016.  
  1017. void GLAPIENTRY
  1018. _mesa_GetVertexAttribiv(GLuint index, GLenum pname, GLint *params)
  1019. {
  1020.    GET_CURRENT_CONTEXT(ctx);
  1021.  
  1022.    if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
  1023.       const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribiv");
  1024.       if (v != NULL) {
  1025.          /* XXX should floats in[0,1] be scaled to full int range? */
  1026.          params[0] = (GLint) v[0];
  1027.          params[1] = (GLint) v[1];
  1028.          params[2] = (GLint) v[2];
  1029.          params[3] = (GLint) v[3];
  1030.       }
  1031.    }
  1032.    else {
  1033.       params[0] = (GLint) get_vertex_array_attrib(ctx, ctx->Array.VAO,
  1034.                                                   index, pname,
  1035.                                                   "glGetVertexAttribiv");
  1036.    }
  1037. }
  1038.  
  1039.  
  1040. /** GL 3.0 */
  1041. void GLAPIENTRY
  1042. _mesa_GetVertexAttribIiv(GLuint index, GLenum pname, GLint *params)
  1043. {
  1044.    GET_CURRENT_CONTEXT(ctx);
  1045.  
  1046.    if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
  1047.       const GLint *v = (const GLint *)
  1048.          get_current_attrib(ctx, index, "glGetVertexAttribIiv");
  1049.       if (v != NULL) {
  1050.          COPY_4V(params, v);
  1051.       }
  1052.    }
  1053.    else {
  1054.       params[0] = (GLint) get_vertex_array_attrib(ctx, ctx->Array.VAO,
  1055.                                                   index, pname,
  1056.                                                   "glGetVertexAttribIiv");
  1057.    }
  1058. }
  1059.  
  1060.  
  1061. /** GL 3.0 */
  1062. void GLAPIENTRY
  1063. _mesa_GetVertexAttribIuiv(GLuint index, GLenum pname, GLuint *params)
  1064. {
  1065.    GET_CURRENT_CONTEXT(ctx);
  1066.  
  1067.    if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
  1068.       const GLuint *v = (const GLuint *)
  1069.          get_current_attrib(ctx, index, "glGetVertexAttribIuiv");
  1070.       if (v != NULL) {
  1071.          COPY_4V(params, v);
  1072.       }
  1073.    }
  1074.    else {
  1075.       params[0] = get_vertex_array_attrib(ctx, ctx->Array.VAO,
  1076.                                           index, pname,
  1077.                                           "glGetVertexAttribIuiv");
  1078.    }
  1079. }
  1080.  
  1081.  
  1082. void GLAPIENTRY
  1083. _mesa_GetVertexAttribPointerv(GLuint index, GLenum pname, GLvoid **pointer)
  1084. {
  1085.    GET_CURRENT_CONTEXT(ctx);
  1086.  
  1087.    if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
  1088.       _mesa_error(ctx, GL_INVALID_VALUE, "glGetVertexAttribPointerARB(index)");
  1089.       return;
  1090.    }
  1091.  
  1092.    if (pname != GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB) {
  1093.       _mesa_error(ctx, GL_INVALID_ENUM, "glGetVertexAttribPointerARB(pname)");
  1094.       return;
  1095.    }
  1096.  
  1097.    assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(ctx->Array.VAO->VertexAttrib));
  1098.  
  1099.    *pointer = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Ptr;
  1100. }
  1101.  
  1102.  
  1103. /** ARB_direct_state_access */
  1104. void GLAPIENTRY
  1105. _mesa_GetVertexArrayIndexediv(GLuint vaobj, GLuint index,
  1106.                               GLenum pname, GLint *params)
  1107. {
  1108.    GET_CURRENT_CONTEXT(ctx);
  1109.    struct gl_vertex_array_object *vao;
  1110.  
  1111.    if (!ctx->Extensions.ARB_direct_state_access) {
  1112.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1113.                    "glGetVertexArrayIndexediv(GL_ARB_direct_state_access "
  1114.                    "is not supported");
  1115.       return;
  1116.    }
  1117.  
  1118.    /* The ARB_direct_state_access specification says:
  1119.     *
  1120.     *    "An INVALID_OPERATION error is generated if <vaobj> is not
  1121.     *     [compatibility profile: zero or] the name of an existing
  1122.     *     vertex array object."
  1123.     */
  1124.    vao = _mesa_lookup_vao_err(ctx, vaobj, "glGetVertexArrayIndexediv");
  1125.    if (!vao)
  1126.       return;
  1127.  
  1128.    /* The ARB_direct_state_access specification says:
  1129.     *
  1130.     *    "For GetVertexArrayIndexediv, <pname> must be one of
  1131.     *     VERTEX_ATTRIB_ARRAY_ENABLED, VERTEX_ATTRIB_ARRAY_SIZE,
  1132.     *     VERTEX_ATTRIB_ARRAY_STRIDE, VERTEX_ATTRIB_ARRAY_TYPE,
  1133.     *     VERTEX_ATTRIB_ARRAY_NORMALIZED, VERTEX_ATTRIB_ARRAY_INTEGER,
  1134.     *     VERTEX_ATTRIB_ARRAY_LONG, VERTEX_ATTRIB_ARRAY_DIVISOR, or
  1135.     *     VERTEX_ATTRIB_RELATIVE_OFFSET."
  1136.     *
  1137.     * and:
  1138.     *
  1139.     *    "Add GetVertexArrayIndexediv in 'Get Command' for
  1140.     *     VERTEX_ATTRIB_ARRAY_BUFFER_BINDING
  1141.     *     VERTEX_ATTRIB_BINDING,
  1142.     *     VERTEX_ATTRIB_RELATIVE_OFFSET,
  1143.     *     VERTEX_BINDING_OFFSET, and
  1144.     *     VERTEX_BINDING_STRIDE states"
  1145.     *
  1146.     * The only parameter name common to both lists is
  1147.     * VERTEX_ATTRIB_RELATIVE_OFFSET.  Also note that VERTEX_BINDING_BUFFER
  1148.     * and VERTEX_BINDING_DIVISOR are missing from both lists.  It seems
  1149.     * pretty clear however that the intent is that it should be possible
  1150.     * to query all vertex attrib and binding states that can be set with
  1151.     * a DSA function.
  1152.     */
  1153.    switch (pname) {
  1154.    case GL_VERTEX_BINDING_OFFSET:
  1155.       params[0] = vao->VertexBinding[VERT_ATTRIB_GENERIC(index)].Offset;
  1156.       break;
  1157.    case GL_VERTEX_BINDING_STRIDE:
  1158.       params[0] = vao->VertexBinding[VERT_ATTRIB_GENERIC(index)].Stride;
  1159.       break;
  1160.    case GL_VERTEX_BINDING_DIVISOR:
  1161.       params[0] = vao->VertexBinding[VERT_ATTRIB_GENERIC(index)].InstanceDivisor;
  1162.       break;
  1163.    case GL_VERTEX_BINDING_BUFFER:
  1164.       params[0] = vao->VertexBinding[VERT_ATTRIB_GENERIC(index)].BufferObj->Name;
  1165.       break;
  1166.    default:
  1167.       params[0] = get_vertex_array_attrib(ctx, vao, index, pname,
  1168.                                           "glGetVertexArrayIndexediv");
  1169.       break;
  1170.    }
  1171. }
  1172.  
  1173.  
  1174. void GLAPIENTRY
  1175. _mesa_GetVertexArrayIndexed64iv(GLuint vaobj, GLuint index,
  1176.                                 GLenum pname, GLint64 *params)
  1177. {
  1178.    GET_CURRENT_CONTEXT(ctx);
  1179.    struct gl_vertex_array_object *vao;
  1180.  
  1181.    if (!ctx->Extensions.ARB_direct_state_access) {
  1182.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1183.                    "glGetVertexArrayIndexed64iv(GL_ARB_direct_state_access "
  1184.                    "is not supported");
  1185.       return;
  1186.    }
  1187.  
  1188.  
  1189.    /* The ARB_direct_state_access specification says:
  1190.     *
  1191.     *    "An INVALID_OPERATION error is generated if <vaobj> is not
  1192.     *     [compatibility profile: zero or] the name of an existing
  1193.     *     vertex array object."
  1194.     */
  1195.    vao = _mesa_lookup_vao_err(ctx, vaobj, "glGetVertexArrayIndexed64iv");
  1196.    if (!vao)
  1197.       return;
  1198.  
  1199.    /* The ARB_direct_state_access specification says:
  1200.     *
  1201.     *    "For GetVertexArrayIndexed64iv, <pname> must be
  1202.     *     VERTEX_BINDING_OFFSET."
  1203.     *
  1204.     * and:
  1205.     *
  1206.     *    "An INVALID_ENUM error is generated if <pname> is not one of
  1207.     *     the valid values listed above for the corresponding command."
  1208.     */
  1209.    if (pname != GL_VERTEX_BINDING_OFFSET) {
  1210.       _mesa_error(ctx, GL_INVALID_ENUM, "glGetVertexArrayIndexed64iv("
  1211.                   "pname != GL_VERTEX_BINDING_OFFSET)");
  1212.       return;
  1213.    }
  1214.  
  1215.    /* The ARB_direct_state_access specification says:
  1216.     *
  1217.     *    "An INVALID_VALUE error is generated if <index> is greater than
  1218.     *     or equal to the value of MAX_VERTEX_ATTRIBS."
  1219.     *
  1220.     * Since the index refers to a buffer binding in this case, the intended
  1221.     * limit must be MAX_VERTEX_ATTRIB_BINDINGS.  Both limits are currently
  1222.     * required to be the same, so in practice this doesn't matter.
  1223.     */
  1224.    if (index >= ctx->Const.MaxVertexAttribBindings) {
  1225.       _mesa_error(ctx, GL_INVALID_VALUE, "glGetVertexArrayIndexed64iv("
  1226.                   "index %d >= the value of GL_MAX_VERTEX_ATTRIB_BINDINGS (%d))",
  1227.                   index, ctx->Const.MaxVertexAttribBindings);
  1228.       return;
  1229.    }
  1230.  
  1231.    params[0] = vao->VertexBinding[VERT_ATTRIB_GENERIC(index)].Offset;
  1232. }
  1233.  
  1234.  
  1235. void GLAPIENTRY
  1236. _mesa_VertexPointerEXT(GLint size, GLenum type, GLsizei stride,
  1237.                        GLsizei count, const GLvoid *ptr)
  1238. {
  1239.    (void) count;
  1240.    _mesa_VertexPointer(size, type, stride, ptr);
  1241. }
  1242.  
  1243.  
  1244. void GLAPIENTRY
  1245. _mesa_NormalPointerEXT(GLenum type, GLsizei stride, GLsizei count,
  1246.                        const GLvoid *ptr)
  1247. {
  1248.    (void) count;
  1249.    _mesa_NormalPointer(type, stride, ptr);
  1250. }
  1251.  
  1252.  
  1253. void GLAPIENTRY
  1254. _mesa_ColorPointerEXT(GLint size, GLenum type, GLsizei stride, GLsizei count,
  1255.                       const GLvoid *ptr)
  1256. {
  1257.    (void) count;
  1258.    _mesa_ColorPointer(size, type, stride, ptr);
  1259. }
  1260.  
  1261.  
  1262. void GLAPIENTRY
  1263. _mesa_IndexPointerEXT(GLenum type, GLsizei stride, GLsizei count,
  1264.                       const GLvoid *ptr)
  1265. {
  1266.    (void) count;
  1267.    _mesa_IndexPointer(type, stride, ptr);
  1268. }
  1269.  
  1270.  
  1271. void GLAPIENTRY
  1272. _mesa_TexCoordPointerEXT(GLint size, GLenum type, GLsizei stride,
  1273.                          GLsizei count, const GLvoid *ptr)
  1274. {
  1275.    (void) count;
  1276.    _mesa_TexCoordPointer(size, type, stride, ptr);
  1277. }
  1278.  
  1279.  
  1280. void GLAPIENTRY
  1281. _mesa_EdgeFlagPointerEXT(GLsizei stride, GLsizei count, const GLboolean *ptr)
  1282. {
  1283.    (void) count;
  1284.    _mesa_EdgeFlagPointer(stride, ptr);
  1285. }
  1286.  
  1287.  
  1288. void GLAPIENTRY
  1289. _mesa_InterleavedArrays(GLenum format, GLsizei stride, const GLvoid *pointer)
  1290. {
  1291.    GET_CURRENT_CONTEXT(ctx);
  1292.    GLboolean tflag, cflag, nflag;  /* enable/disable flags */
  1293.    GLint tcomps, ccomps, vcomps;   /* components per texcoord, color, vertex */
  1294.    GLenum ctype = 0;               /* color type */
  1295.    GLint coffset = 0, noffset = 0, voffset;/* color, normal, vertex offsets */
  1296.    const GLint toffset = 0;        /* always zero */
  1297.    GLint defstride;                /* default stride */
  1298.    GLint c, f;
  1299.  
  1300.    FLUSH_VERTICES(ctx, 0);
  1301.  
  1302.    f = sizeof(GLfloat);
  1303.    c = f * ((4 * sizeof(GLubyte) + (f - 1)) / f);
  1304.  
  1305.    if (stride < 0) {
  1306.       _mesa_error( ctx, GL_INVALID_VALUE, "glInterleavedArrays(stride)" );
  1307.       return;
  1308.    }
  1309.  
  1310.    switch (format) {
  1311.       case GL_V2F:
  1312.          tflag = GL_FALSE;  cflag = GL_FALSE;  nflag = GL_FALSE;
  1313.          tcomps = 0;  ccomps = 0;  vcomps = 2;
  1314.          voffset = 0;
  1315.          defstride = 2*f;
  1316.          break;
  1317.       case GL_V3F:
  1318.          tflag = GL_FALSE;  cflag = GL_FALSE;  nflag = GL_FALSE;
  1319.          tcomps = 0;  ccomps = 0;  vcomps = 3;
  1320.          voffset = 0;
  1321.          defstride = 3*f;
  1322.          break;
  1323.       case GL_C4UB_V2F:
  1324.          tflag = GL_FALSE;  cflag = GL_TRUE;  nflag = GL_FALSE;
  1325.          tcomps = 0;  ccomps = 4;  vcomps = 2;
  1326.          ctype = GL_UNSIGNED_BYTE;
  1327.          coffset = 0;
  1328.          voffset = c;
  1329.          defstride = c + 2*f;
  1330.          break;
  1331.       case GL_C4UB_V3F:
  1332.          tflag = GL_FALSE;  cflag = GL_TRUE;  nflag = GL_FALSE;
  1333.          tcomps = 0;  ccomps = 4;  vcomps = 3;
  1334.          ctype = GL_UNSIGNED_BYTE;
  1335.          coffset = 0;
  1336.          voffset = c;
  1337.          defstride = c + 3*f;
  1338.          break;
  1339.       case GL_C3F_V3F:
  1340.          tflag = GL_FALSE;  cflag = GL_TRUE;  nflag = GL_FALSE;
  1341.          tcomps = 0;  ccomps = 3;  vcomps = 3;
  1342.          ctype = GL_FLOAT;
  1343.          coffset = 0;
  1344.          voffset = 3*f;
  1345.          defstride = 6*f;
  1346.          break;
  1347.       case GL_N3F_V3F:
  1348.          tflag = GL_FALSE;  cflag = GL_FALSE;  nflag = GL_TRUE;
  1349.          tcomps = 0;  ccomps = 0;  vcomps = 3;
  1350.          noffset = 0;
  1351.          voffset = 3*f;
  1352.          defstride = 6*f;
  1353.          break;
  1354.       case GL_C4F_N3F_V3F:
  1355.          tflag = GL_FALSE;  cflag = GL_TRUE;  nflag = GL_TRUE;
  1356.          tcomps = 0;  ccomps = 4;  vcomps = 3;
  1357.          ctype = GL_FLOAT;
  1358.          coffset = 0;
  1359.          noffset = 4*f;
  1360.          voffset = 7*f;
  1361.          defstride = 10*f;
  1362.          break;
  1363.       case GL_T2F_V3F:
  1364.          tflag = GL_TRUE;  cflag = GL_FALSE;  nflag = GL_FALSE;
  1365.          tcomps = 2;  ccomps = 0;  vcomps = 3;
  1366.          voffset = 2*f;
  1367.          defstride = 5*f;
  1368.          break;
  1369.       case GL_T4F_V4F:
  1370.          tflag = GL_TRUE;  cflag = GL_FALSE;  nflag = GL_FALSE;
  1371.          tcomps = 4;  ccomps = 0;  vcomps = 4;
  1372.          voffset = 4*f;
  1373.          defstride = 8*f;
  1374.          break;
  1375.       case GL_T2F_C4UB_V3F:
  1376.          tflag = GL_TRUE;  cflag = GL_TRUE;  nflag = GL_FALSE;
  1377.          tcomps = 2;  ccomps = 4;  vcomps = 3;
  1378.          ctype = GL_UNSIGNED_BYTE;
  1379.          coffset = 2*f;
  1380.          voffset = c+2*f;
  1381.          defstride = c+5*f;
  1382.          break;
  1383.       case GL_T2F_C3F_V3F:
  1384.          tflag = GL_TRUE;  cflag = GL_TRUE;  nflag = GL_FALSE;
  1385.          tcomps = 2;  ccomps = 3;  vcomps = 3;
  1386.          ctype = GL_FLOAT;
  1387.          coffset = 2*f;
  1388.          voffset = 5*f;
  1389.          defstride = 8*f;
  1390.          break;
  1391.       case GL_T2F_N3F_V3F:
  1392.          tflag = GL_TRUE;  cflag = GL_FALSE;  nflag = GL_TRUE;
  1393.          tcomps = 2;  ccomps = 0;  vcomps = 3;
  1394.          noffset = 2*f;
  1395.          voffset = 5*f;
  1396.          defstride = 8*f;
  1397.          break;
  1398.       case GL_T2F_C4F_N3F_V3F:
  1399.          tflag = GL_TRUE;  cflag = GL_TRUE;  nflag = GL_TRUE;
  1400.          tcomps = 2;  ccomps = 4;  vcomps = 3;
  1401.          ctype = GL_FLOAT;
  1402.          coffset = 2*f;
  1403.          noffset = 6*f;
  1404.          voffset = 9*f;
  1405.          defstride = 12*f;
  1406.          break;
  1407.       case GL_T4F_C4F_N3F_V4F:
  1408.          tflag = GL_TRUE;  cflag = GL_TRUE;  nflag = GL_TRUE;
  1409.          tcomps = 4;  ccomps = 4;  vcomps = 4;
  1410.          ctype = GL_FLOAT;
  1411.          coffset = 4*f;
  1412.          noffset = 8*f;
  1413.          voffset = 11*f;
  1414.          defstride = 15*f;
  1415.          break;
  1416.       default:
  1417.          _mesa_error( ctx, GL_INVALID_ENUM, "glInterleavedArrays(format)" );
  1418.          return;
  1419.    }
  1420.  
  1421.    if (stride==0) {
  1422.       stride = defstride;
  1423.    }
  1424.  
  1425.    _mesa_DisableClientState( GL_EDGE_FLAG_ARRAY );
  1426.    _mesa_DisableClientState( GL_INDEX_ARRAY );
  1427.    /* XXX also disable secondary color and generic arrays? */
  1428.  
  1429.    /* Texcoords */
  1430.    if (tflag) {
  1431.       _mesa_EnableClientState( GL_TEXTURE_COORD_ARRAY );
  1432.       _mesa_TexCoordPointer( tcomps, GL_FLOAT, stride,
  1433.                              (GLubyte *) pointer + toffset );
  1434.    }
  1435.    else {
  1436.       _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY );
  1437.    }
  1438.  
  1439.    /* Color */
  1440.    if (cflag) {
  1441.       _mesa_EnableClientState( GL_COLOR_ARRAY );
  1442.       _mesa_ColorPointer( ccomps, ctype, stride,
  1443.                           (GLubyte *) pointer + coffset );
  1444.    }
  1445.    else {
  1446.       _mesa_DisableClientState( GL_COLOR_ARRAY );
  1447.    }
  1448.  
  1449.  
  1450.    /* Normals */
  1451.    if (nflag) {
  1452.       _mesa_EnableClientState( GL_NORMAL_ARRAY );
  1453.       _mesa_NormalPointer( GL_FLOAT, stride, (GLubyte *) pointer + noffset );
  1454.    }
  1455.    else {
  1456.       _mesa_DisableClientState( GL_NORMAL_ARRAY );
  1457.    }
  1458.  
  1459.    /* Vertices */
  1460.    _mesa_EnableClientState( GL_VERTEX_ARRAY );
  1461.    _mesa_VertexPointer( vcomps, GL_FLOAT, stride,
  1462.                         (GLubyte *) pointer + voffset );
  1463. }
  1464.  
  1465.  
  1466. void GLAPIENTRY
  1467. _mesa_LockArraysEXT(GLint first, GLsizei count)
  1468. {
  1469.    GET_CURRENT_CONTEXT(ctx);
  1470.  
  1471.    FLUSH_VERTICES(ctx, 0);
  1472.  
  1473.    if (MESA_VERBOSE & VERBOSE_API)
  1474.       _mesa_debug(ctx, "glLockArrays %d %d\n", first, count);
  1475.  
  1476.    if (first < 0) {
  1477.       _mesa_error( ctx, GL_INVALID_VALUE, "glLockArraysEXT(first)" );
  1478.       return;
  1479.    }
  1480.    if (count <= 0) {
  1481.       _mesa_error( ctx, GL_INVALID_VALUE, "glLockArraysEXT(count)" );
  1482.       return;
  1483.    }
  1484.    if (ctx->Array.LockCount != 0) {
  1485.       _mesa_error( ctx, GL_INVALID_OPERATION, "glLockArraysEXT(reentry)" );
  1486.       return;
  1487.    }
  1488.  
  1489.    ctx->Array.LockFirst = first;
  1490.    ctx->Array.LockCount = count;
  1491.  
  1492.    ctx->NewState |= _NEW_ARRAY;
  1493. }
  1494.  
  1495.  
  1496. void GLAPIENTRY
  1497. _mesa_UnlockArraysEXT( void )
  1498. {
  1499.    GET_CURRENT_CONTEXT(ctx);
  1500.  
  1501.    FLUSH_VERTICES(ctx, 0);
  1502.  
  1503.    if (MESA_VERBOSE & VERBOSE_API)
  1504.       _mesa_debug(ctx, "glUnlockArrays\n");
  1505.  
  1506.    if (ctx->Array.LockCount == 0) {
  1507.       _mesa_error( ctx, GL_INVALID_OPERATION, "glUnlockArraysEXT(reexit)" );
  1508.       return;
  1509.    }
  1510.  
  1511.    ctx->Array.LockFirst = 0;
  1512.    ctx->Array.LockCount = 0;
  1513.    ctx->NewState |= _NEW_ARRAY;
  1514. }
  1515.  
  1516.  
  1517. /* GL_EXT_multi_draw_arrays */
  1518. void GLAPIENTRY
  1519. _mesa_MultiDrawArrays( GLenum mode, const GLint *first,
  1520.                           const GLsizei *count, GLsizei primcount )
  1521. {
  1522.    GET_CURRENT_CONTEXT(ctx);
  1523.    GLint i;
  1524.  
  1525.    FLUSH_VERTICES(ctx, 0);
  1526.  
  1527.    for (i = 0; i < primcount; i++) {
  1528.       if (count[i] > 0) {
  1529.          CALL_DrawArrays(ctx->CurrentDispatch, (mode, first[i], count[i]));
  1530.       }
  1531.    }
  1532. }
  1533.  
  1534.  
  1535. /* GL_IBM_multimode_draw_arrays */
  1536. void GLAPIENTRY
  1537. _mesa_MultiModeDrawArraysIBM( const GLenum * mode, const GLint * first,
  1538.                               const GLsizei * count,
  1539.                               GLsizei primcount, GLint modestride )
  1540. {
  1541.    GET_CURRENT_CONTEXT(ctx);
  1542.    GLint i;
  1543.  
  1544.    FLUSH_VERTICES(ctx, 0);
  1545.  
  1546.    for ( i = 0 ; i < primcount ; i++ ) {
  1547.       if ( count[i] > 0 ) {
  1548.          GLenum m = *((GLenum *) ((GLubyte *) mode + i * modestride));
  1549.          CALL_DrawArrays(ctx->CurrentDispatch, ( m, first[i], count[i] ));
  1550.       }
  1551.    }
  1552. }
  1553.  
  1554.  
  1555. /* GL_IBM_multimode_draw_arrays */
  1556. void GLAPIENTRY
  1557. _mesa_MultiModeDrawElementsIBM( const GLenum * mode, const GLsizei * count,
  1558.                                 GLenum type, const GLvoid * const * indices,
  1559.                                 GLsizei primcount, GLint modestride )
  1560. {
  1561.    GET_CURRENT_CONTEXT(ctx);
  1562.    GLint i;
  1563.  
  1564.    FLUSH_VERTICES(ctx, 0);
  1565.  
  1566.    /* XXX not sure about ARB_vertex_buffer_object handling here */
  1567.  
  1568.    for ( i = 0 ; i < primcount ; i++ ) {
  1569.       if ( count[i] > 0 ) {
  1570.          GLenum m = *((GLenum *) ((GLubyte *) mode + i * modestride));
  1571.          CALL_DrawElements(ctx->CurrentDispatch, ( m, count[i], type,
  1572.                                                    indices[i] ));
  1573.       }
  1574.    }
  1575. }
  1576.  
  1577.  
  1578. /**
  1579.  * GL_NV_primitive_restart and GL 3.1
  1580.  */
  1581. void GLAPIENTRY
  1582. _mesa_PrimitiveRestartIndex(GLuint index)
  1583. {
  1584.    GET_CURRENT_CONTEXT(ctx);
  1585.  
  1586.    if (!ctx->Extensions.NV_primitive_restart && ctx->Version < 31) {
  1587.       _mesa_error(ctx, GL_INVALID_OPERATION, "glPrimitiveRestartIndexNV()");
  1588.       return;
  1589.    }
  1590.  
  1591.    if (ctx->Array.RestartIndex != index) {
  1592.       FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
  1593.       ctx->Array.RestartIndex = index;
  1594.    }
  1595. }
  1596.  
  1597.  
  1598. /**
  1599.  * See GL_ARB_instanced_arrays.
  1600.  * Note that the instance divisor only applies to generic arrays, not
  1601.  * the legacy vertex arrays.
  1602.  */
  1603. void GLAPIENTRY
  1604. _mesa_VertexAttribDivisor(GLuint index, GLuint divisor)
  1605. {
  1606.    GET_CURRENT_CONTEXT(ctx);
  1607.  
  1608.    const GLuint genericIndex = VERT_ATTRIB_GENERIC(index);
  1609.    struct gl_vertex_array_object * const vao = ctx->Array.VAO;
  1610.  
  1611.    if (!ctx->Extensions.ARB_instanced_arrays) {
  1612.       _mesa_error(ctx, GL_INVALID_OPERATION, "glVertexAttribDivisor()");
  1613.       return;
  1614.    }
  1615.  
  1616.    if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
  1617.       _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribDivisor(index = %u)",
  1618.                   index);
  1619.       return;
  1620.    }
  1621.  
  1622.    assert(genericIndex < ARRAY_SIZE(vao->VertexAttrib));
  1623.  
  1624.    /* The ARB_vertex_attrib_binding spec says:
  1625.     *
  1626.     *    "The command
  1627.     *
  1628.     *       void VertexAttribDivisor(uint index, uint divisor);
  1629.     *
  1630.     *     is equivalent to (assuming no errors are generated):
  1631.     *
  1632.     *       VertexAttribBinding(index, index);
  1633.     *       VertexBindingDivisor(index, divisor);"
  1634.     */
  1635.    vertex_attrib_binding(ctx, vao, genericIndex, genericIndex);
  1636.    vertex_binding_divisor(ctx, vao, genericIndex, divisor);
  1637. }
  1638.  
  1639.  
  1640. unsigned
  1641. _mesa_primitive_restart_index(const struct gl_context *ctx, GLenum ib_type)
  1642. {
  1643.    /* From the OpenGL 4.3 core specification, page 302:
  1644.     * "If both PRIMITIVE_RESTART and PRIMITIVE_RESTART_FIXED_INDEX are
  1645.     *  enabled, the index value determined by PRIMITIVE_RESTART_FIXED_INDEX
  1646.     *  is used."
  1647.     */
  1648.    if (ctx->Array.PrimitiveRestartFixedIndex) {
  1649.       switch (ib_type) {
  1650.       case GL_UNSIGNED_BYTE:
  1651.          return 0xff;
  1652.       case GL_UNSIGNED_SHORT:
  1653.          return 0xffff;
  1654.       case GL_UNSIGNED_INT:
  1655.          return 0xffffffff;
  1656.       default:
  1657.          assert(!"_mesa_primitive_restart_index: Invalid index buffer type.");
  1658.       }
  1659.    }
  1660.  
  1661.    return ctx->Array.RestartIndex;
  1662. }
  1663.  
  1664.  
  1665. /**
  1666.  * GL_ARB_vertex_attrib_binding
  1667.  */
  1668. static void
  1669. vertex_array_vertex_buffer(struct gl_context *ctx, struct gl_vertex_array_object *vao,
  1670.                            GLuint bindingIndex, GLuint buffer, GLintptr offset,
  1671.                            GLsizei stride, const char *func)
  1672. {
  1673.    struct gl_buffer_object *vbo;
  1674.  
  1675.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  1676.  
  1677.    /* The ARB_vertex_attrib_binding spec says:
  1678.     *
  1679.     *    "An INVALID_VALUE error is generated if <bindingindex> is greater than
  1680.     *     the value of MAX_VERTEX_ATTRIB_BINDINGS."
  1681.     */
  1682.    if (bindingIndex >= ctx->Const.MaxVertexAttribBindings) {
  1683.       _mesa_error(ctx, GL_INVALID_VALUE,
  1684.                   "%s(bindingindex=%u > "
  1685.                   "GL_MAX_VERTEX_ATTRIB_BINDINGS)",
  1686.                   func, bindingIndex);
  1687.       return;
  1688.    }
  1689.  
  1690.    /* The ARB_vertex_attrib_binding spec says:
  1691.     *
  1692.     *    "The error INVALID_VALUE is generated if <stride> or <offset>
  1693.     *     are negative."
  1694.     */
  1695.    if (offset < 0) {
  1696.       _mesa_error(ctx, GL_INVALID_VALUE,
  1697.                   "%s(offset=%" PRId64 " < 0)",
  1698.                   func, (int64_t) offset);
  1699.       return;
  1700.    }
  1701.  
  1702.    if (stride < 0) {
  1703.       _mesa_error(ctx, GL_INVALID_VALUE,
  1704.                   "%s(stride=%d < 0)", func, stride);
  1705.       return;
  1706.    }
  1707.  
  1708.    if (ctx->API == API_OPENGL_CORE && ctx->Version >= 44 &&
  1709.        stride > ctx->Const.MaxVertexAttribStride) {
  1710.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(stride=%d > "
  1711.                   "GL_MAX_VERTEX_ATTRIB_STRIDE)", func, stride);
  1712.       return;
  1713.    }
  1714.  
  1715.    if (buffer == vao->VertexBinding[VERT_ATTRIB_GENERIC(bindingIndex)].BufferObj->Name) {
  1716.       vbo = vao->VertexBinding[VERT_ATTRIB_GENERIC(bindingIndex)].BufferObj;
  1717.    } else if (buffer != 0) {
  1718.       vbo = _mesa_lookup_bufferobj(ctx, buffer);
  1719.  
  1720.       /* From the GL_ARB_vertex_attrib_array spec:
  1721.        *
  1722.        *   "[Core profile only:]
  1723.        *    An INVALID_OPERATION error is generated if buffer is not zero or a
  1724.        *    name returned from a previous call to GenBuffers, or if such a name
  1725.        *    has since been deleted with DeleteBuffers.
  1726.        *
  1727.        * Otherwise, we fall back to the same compat profile behavior as other
  1728.        * object references (automatically gen it).
  1729.        */
  1730.       if (!_mesa_handle_bind_buffer_gen(ctx, GL_ARRAY_BUFFER, buffer,
  1731.                                         &vbo, func))
  1732.          return;
  1733.    } else {
  1734.       /* The ARB_vertex_attrib_binding spec says:
  1735.        *
  1736.        *    "If <buffer> is zero, any buffer object attached to this
  1737.        *     bindpoint is detached."
  1738.        */
  1739.       vbo = ctx->Shared->NullBufferObj;
  1740.    }
  1741.  
  1742.    bind_vertex_buffer(ctx, vao, VERT_ATTRIB_GENERIC(bindingIndex),
  1743.                       vbo, offset, stride);
  1744. }
  1745.  
  1746.  
  1747. void GLAPIENTRY
  1748. _mesa_BindVertexBuffer(GLuint bindingIndex, GLuint buffer, GLintptr offset,
  1749.                        GLsizei stride)
  1750. {
  1751.    GET_CURRENT_CONTEXT(ctx);
  1752.  
  1753.    /* The ARB_vertex_attrib_binding spec says:
  1754.     *
  1755.     *    "An INVALID_OPERATION error is generated if no vertex array object
  1756.     *     is bound."
  1757.     */
  1758.    if (ctx->API == API_OPENGL_CORE &&
  1759.        ctx->Array.VAO == ctx->Array.DefaultVAO) {
  1760.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1761.                   "glBindVertexBuffer(No array object bound)");
  1762.       return;
  1763.    }
  1764.  
  1765.    vertex_array_vertex_buffer(ctx, ctx->Array.VAO, bindingIndex,
  1766.                               buffer, offset, stride, "glBindVertexBuffer");
  1767. }
  1768.  
  1769.  
  1770. void GLAPIENTRY
  1771. _mesa_VertexArrayVertexBuffer(GLuint vaobj, GLuint bindingIndex, GLuint buffer,
  1772.                               GLintptr offset, GLsizei stride)
  1773. {
  1774.    GET_CURRENT_CONTEXT(ctx);
  1775.    struct gl_vertex_array_object *vao;
  1776.  
  1777.    if (!ctx->Extensions.ARB_direct_state_access) {
  1778.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1779.                    "glVertexArrayVertexBuffer(GL_ARB_direct_state_access "
  1780.                    "is not supported");
  1781.       return;
  1782.    }
  1783.  
  1784.    /* The ARB_direct_state_access specification says:
  1785.     *
  1786.     *   "An INVALID_OPERATION error is generated by VertexArrayVertexBuffer
  1787.     *    if <vaobj> is not [compatibility profile: zero or] the name of an
  1788.     *    existing vertex array object."
  1789.     */
  1790.    vao = _mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayVertexBuffer");
  1791.    if (!vao)
  1792.       return;
  1793.  
  1794.    vertex_array_vertex_buffer(ctx, vao, bindingIndex,
  1795.                               buffer, offset, stride,
  1796.                               "glVertexArrayVertexBuffer");
  1797. }
  1798.  
  1799.  
  1800. static void
  1801. vertex_array_vertex_buffers(struct gl_context *ctx,
  1802.                             struct gl_vertex_array_object *vao,
  1803.                             GLuint first, GLsizei count, const GLuint *buffers,
  1804.                             const GLintptr *offsets, const GLsizei *strides,
  1805.                             const char *func)
  1806. {
  1807.    GLuint i;
  1808.  
  1809.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  1810.  
  1811.    /* The ARB_multi_bind spec says:
  1812.     *
  1813.     *    "An INVALID_OPERATION error is generated if <first> + <count>
  1814.     *     is greater than the value of MAX_VERTEX_ATTRIB_BINDINGS."
  1815.     */
  1816.    if (first + count > ctx->Const.MaxVertexAttribBindings) {
  1817.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1818.                   "%s(first=%u + count=%d > the value of "
  1819.                   "GL_MAX_VERTEX_ATTRIB_BINDINGS=%u)",
  1820.                   func, first, count, ctx->Const.MaxVertexAttribBindings);
  1821.       return;
  1822.    }
  1823.  
  1824.    if (!buffers) {
  1825.       /**
  1826.        * The ARB_multi_bind spec says:
  1827.        *
  1828.        *    "If <buffers> is NULL, each affected vertex buffer binding point
  1829.        *     from <first> through <first>+<count>-1 will be reset to have no
  1830.        *     bound buffer object.  In this case, the offsets and strides
  1831.        *     associated with the binding points are set to default values,
  1832.        *     ignoring <offsets> and <strides>."
  1833.        */
  1834.       struct gl_buffer_object *vbo = ctx->Shared->NullBufferObj;
  1835.  
  1836.       for (i = 0; i < count; i++)
  1837.          bind_vertex_buffer(ctx, vao, VERT_ATTRIB_GENERIC(first + i),
  1838.                             vbo, 0, 16);
  1839.  
  1840.       return;
  1841.    }
  1842.  
  1843.    /* Note that the error semantics for multi-bind commands differ from
  1844.     * those of other GL commands.
  1845.     *
  1846.     * The Issues section in the ARB_multi_bind spec says:
  1847.     *
  1848.     *    "(11) Typically, OpenGL specifies that if an error is generated by
  1849.     *          a command, that command has no effect.  This is somewhat
  1850.     *          unfortunate for multi-bind commands, because it would require
  1851.     *          a first pass to scan the entire list of bound objects for
  1852.     *          errors and then a second pass to actually perform the
  1853.     *          bindings.  Should we have different error semantics?
  1854.     *
  1855.     *       RESOLVED:  Yes.  In this specification, when the parameters for
  1856.     *       one of the <count> binding points are invalid, that binding
  1857.     *       point is not updated and an error will be generated.  However,
  1858.     *       other binding points in the same command will be updated if
  1859.     *       their parameters are valid and no other error occurs."
  1860.     */
  1861.  
  1862.    _mesa_begin_bufferobj_lookups(ctx);
  1863.  
  1864.    for (i = 0; i < count; i++) {
  1865.       struct gl_buffer_object *vbo;
  1866.  
  1867.       /* The ARB_multi_bind spec says:
  1868.        *
  1869.        *    "An INVALID_VALUE error is generated if any value in
  1870.        *     <offsets> or <strides> is negative (per binding)."
  1871.        */
  1872.       if (offsets[i] < 0) {
  1873.          _mesa_error(ctx, GL_INVALID_VALUE,
  1874.                      "%s(offsets[%u]=%" PRId64 " < 0)",
  1875.                      func, i, (int64_t) offsets[i]);
  1876.          continue;
  1877.       }
  1878.  
  1879.       if (strides[i] < 0) {
  1880.          _mesa_error(ctx, GL_INVALID_VALUE,
  1881.                      "%s(strides[%u]=%d < 0)",
  1882.                      func, i, strides[i]);
  1883.          continue;
  1884.       }
  1885.  
  1886.       if (ctx->API == API_OPENGL_CORE && ctx->Version >= 44 &&
  1887.           strides[i] > ctx->Const.MaxVertexAttribStride) {
  1888.          _mesa_error(ctx, GL_INVALID_VALUE,
  1889.                      "%s(strides[%u]=%d > "
  1890.                      "GL_MAX_VERTEX_ATTRIB_STRIDE)", func, i, strides[i]);
  1891.          continue;
  1892.       }
  1893.  
  1894.       if (buffers[i]) {
  1895.          struct gl_vertex_buffer_binding *binding =
  1896.             &vao->VertexBinding[VERT_ATTRIB_GENERIC(first + i)];
  1897.  
  1898.          if (buffers[i] == binding->BufferObj->Name)
  1899.             vbo = binding->BufferObj;
  1900.          else
  1901.             vbo = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, func);
  1902.  
  1903.          if (!vbo)
  1904.             continue;
  1905.       } else {
  1906.          vbo = ctx->Shared->NullBufferObj;
  1907.       }
  1908.  
  1909.       bind_vertex_buffer(ctx, vao, VERT_ATTRIB_GENERIC(first + i),
  1910.                          vbo, offsets[i], strides[i]);
  1911.    }
  1912.  
  1913.    _mesa_end_bufferobj_lookups(ctx);
  1914. }
  1915.  
  1916.  
  1917. void GLAPIENTRY
  1918. _mesa_BindVertexBuffers(GLuint first, GLsizei count, const GLuint *buffers,
  1919.                         const GLintptr *offsets, const GLsizei *strides)
  1920. {
  1921.    GET_CURRENT_CONTEXT(ctx);
  1922.  
  1923.    /* The ARB_vertex_attrib_binding spec says:
  1924.     *
  1925.     *    "An INVALID_OPERATION error is generated if no
  1926.     *     vertex array object is bound."
  1927.     */
  1928.    if (ctx->API == API_OPENGL_CORE &&
  1929.        ctx->Array.VAO == ctx->Array.DefaultVAO) {
  1930.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1931.                   "glBindVertexBuffers(No array object bound)");
  1932.       return;
  1933.    }
  1934.  
  1935.    vertex_array_vertex_buffers(ctx, ctx->Array.VAO, first, count,
  1936.                                buffers, offsets, strides,
  1937.                                "glBindVertexBuffers");
  1938. }
  1939.  
  1940.  
  1941. void GLAPIENTRY
  1942. _mesa_VertexArrayVertexBuffers(GLuint vaobj, GLuint first, GLsizei count,
  1943.                                const GLuint *buffers,
  1944.                                const GLintptr *offsets, const GLsizei *strides)
  1945. {
  1946.    GET_CURRENT_CONTEXT(ctx);
  1947.    struct gl_vertex_array_object *vao;
  1948.  
  1949.    if (!ctx->Extensions.ARB_direct_state_access) {
  1950.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1951.                    "glVertexArrayVertexBuffers(GL_ARB_direct_state_access "
  1952.                    "is not supported");
  1953.       return;
  1954.    }
  1955.  
  1956.  
  1957.    /* The ARB_direct_state_access specification says:
  1958.     *
  1959.     *   "An INVALID_OPERATION error is generated by VertexArrayVertexBuffer
  1960.     *    if <vaobj> is not [compatibility profile: zero or] the name of an
  1961.     *    existing vertex array object."
  1962.     */
  1963.    vao = _mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayVertexBuffers");
  1964.    if (!vao)
  1965.       return;
  1966.  
  1967.    vertex_array_vertex_buffers(ctx, vao, first, count,
  1968.                                buffers, offsets, strides,
  1969.                                "glVertexArrayVertexBuffers");
  1970. }
  1971.  
  1972.  
  1973. static void
  1974. vertex_attrib_format(GLuint attribIndex, GLint size, GLenum type,
  1975.                      GLboolean normalized, GLboolean integer,
  1976.                      GLboolean doubles, GLbitfield legalTypes,
  1977.                      GLsizei maxSize, GLuint relativeOffset,
  1978.                      const char *func)
  1979. {
  1980.    GET_CURRENT_CONTEXT(ctx);
  1981.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  1982.  
  1983.    /* The ARB_vertex_attrib_binding spec says:
  1984.     *
  1985.     *    "An INVALID_OPERATION error is generated under any of the following
  1986.     *     conditions:
  1987.     *     - if no vertex array object is currently bound (see section 2.10);
  1988.     *     - ..."
  1989.     *
  1990.     * This error condition only applies to VertexAttribFormat and
  1991.     * VertexAttribIFormat in the extension spec, but we assume that this
  1992.     * is an oversight.  In the OpenGL 4.3 (Core Profile) spec, it applies
  1993.     * to all three functions.
  1994.     */
  1995.    if (ctx->API == API_OPENGL_CORE &&
  1996.        ctx->Array.VAO == ctx->Array.DefaultVAO) {
  1997.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1998.                   "%s(No array object bound)", func);
  1999.       return;
  2000.    }
  2001.  
  2002.    /* The ARB_vertex_attrib_binding spec says:
  2003.     *
  2004.     *   "The error INVALID_VALUE is generated if index is greater than or equal
  2005.     *     to the value of MAX_VERTEX_ATTRIBS."
  2006.     */
  2007.    if (attribIndex >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
  2008.       _mesa_error(ctx, GL_INVALID_VALUE,
  2009.                   "%s(attribindex=%u > "
  2010.                   "GL_MAX_VERTEX_ATTRIBS)",
  2011.                   func, attribIndex);
  2012.       return;
  2013.    }
  2014.  
  2015.    FLUSH_VERTICES(ctx, 0);
  2016.  
  2017.    update_array_format(ctx, func, ctx->Array.VAO,
  2018.                        VERT_ATTRIB_GENERIC(attribIndex),
  2019.                        legalTypes, 1, maxSize, size, type,
  2020.                        normalized, integer, doubles, relativeOffset);
  2021. }
  2022.  
  2023.  
  2024. void GLAPIENTRY
  2025. _mesa_VertexAttribFormat(GLuint attribIndex, GLint size, GLenum type,
  2026.                          GLboolean normalized, GLuint relativeOffset)
  2027. {
  2028.    vertex_attrib_format(attribIndex, size, type, normalized,
  2029.                         GL_FALSE, GL_FALSE, ATTRIB_FORMAT_TYPES_MASK,
  2030.                         BGRA_OR_4, relativeOffset,
  2031.                         "glVertexAttribFormat");
  2032. }
  2033.  
  2034.  
  2035. void GLAPIENTRY
  2036. _mesa_VertexAttribIFormat(GLuint attribIndex, GLint size, GLenum type,
  2037.                           GLuint relativeOffset)
  2038. {
  2039.    vertex_attrib_format(attribIndex, size, type, GL_FALSE,
  2040.                         GL_TRUE, GL_FALSE, ATTRIB_IFORMAT_TYPES_MASK, 4,
  2041.                         relativeOffset, "glVertexAttribIFormat");
  2042. }
  2043.  
  2044.  
  2045. void GLAPIENTRY
  2046. _mesa_VertexAttribLFormat(GLuint attribIndex, GLint size, GLenum type,
  2047.                           GLuint relativeOffset)
  2048. {
  2049.    vertex_attrib_format(attribIndex, size, type, GL_FALSE, GL_FALSE,
  2050.                         GL_TRUE, ATTRIB_LFORMAT_TYPES_MASK, 4,
  2051.                         relativeOffset, "glVertexAttribLFormat");
  2052. }
  2053.  
  2054.  
  2055. static void
  2056. vertex_array_attrib_format(GLuint vaobj, GLuint attribIndex, GLint size,
  2057.                            GLenum type, GLboolean normalized,
  2058.                            GLboolean integer, GLboolean doubles,
  2059.                            GLbitfield legalTypes, GLsizei maxSize,
  2060.                            GLuint relativeOffset, const char *func)
  2061. {
  2062.    GET_CURRENT_CONTEXT(ctx);
  2063.    struct gl_vertex_array_object *vao;
  2064.  
  2065.    if (!ctx->Extensions.ARB_direct_state_access) {
  2066.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2067.                    "%s(GL_ARB_direct_state_access is not supported", func);
  2068.       return;
  2069.    }
  2070.  
  2071.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  2072.  
  2073.    /* The ARB_direct_state_access spec says:
  2074.     *
  2075.     *   "An INVALID_OPERATION error is generated by VertexArrayAttrib*Format
  2076.     *    if <vaobj> is not [compatibility profile: zero or] the name of an
  2077.     *    existing vertex array object."
  2078.     */
  2079.    vao = _mesa_lookup_vao_err(ctx, vaobj, func);
  2080.    if (!vao)
  2081.       return;
  2082.  
  2083.    /* The ARB_vertex_attrib_binding spec says:
  2084.     *
  2085.     *   "The error INVALID_VALUE is generated if index is greater than or equal
  2086.     *    to the value of MAX_VERTEX_ATTRIBS."
  2087.     */
  2088.    if (attribIndex >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
  2089.       _mesa_error(ctx, GL_INVALID_VALUE,
  2090.                   "%s(attribindex=%u > GL_MAX_VERTEX_ATTRIBS)",
  2091.                   func, attribIndex);
  2092.       return;
  2093.    }
  2094.  
  2095.    FLUSH_VERTICES(ctx, 0);
  2096.  
  2097.    update_array_format(ctx, func, vao,
  2098.                        VERT_ATTRIB_GENERIC(attribIndex),
  2099.                        legalTypes, 1, maxSize, size, type, normalized,
  2100.                        integer, doubles, relativeOffset);
  2101. }
  2102.  
  2103.  
  2104. void GLAPIENTRY
  2105. _mesa_VertexArrayAttribFormat(GLuint vaobj, GLuint attribIndex, GLint size,
  2106.                               GLenum type, GLboolean normalized,
  2107.                               GLuint relativeOffset)
  2108. {
  2109.    vertex_array_attrib_format(vaobj, attribIndex, size, type, normalized,
  2110.                               GL_FALSE, GL_FALSE, ATTRIB_FORMAT_TYPES_MASK,
  2111.                               BGRA_OR_4, relativeOffset,
  2112.                               "glVertexArrayAttribFormat");
  2113. }
  2114.  
  2115.  
  2116. void GLAPIENTRY
  2117. _mesa_VertexArrayAttribIFormat(GLuint vaobj, GLuint attribIndex,
  2118.                                GLint size, GLenum type,
  2119.                                GLuint relativeOffset)
  2120. {
  2121.    vertex_array_attrib_format(vaobj, attribIndex, size, type, GL_FALSE,
  2122.                               GL_TRUE, GL_FALSE, ATTRIB_IFORMAT_TYPES_MASK,
  2123.                               4, relativeOffset,
  2124.                               "glVertexArrayAttribIFormat");
  2125. }
  2126.  
  2127.  
  2128. void GLAPIENTRY
  2129. _mesa_VertexArrayAttribLFormat(GLuint vaobj, GLuint attribIndex,
  2130.                                GLint size, GLenum type,
  2131.                                GLuint relativeOffset)
  2132. {
  2133.    vertex_array_attrib_format(vaobj, attribIndex, size, type, GL_FALSE,
  2134.                               GL_FALSE, GL_TRUE, ATTRIB_LFORMAT_TYPES_MASK,
  2135.                               4, relativeOffset,
  2136.                               "glVertexArrayAttribLFormat");
  2137. }
  2138.  
  2139.  
  2140. static void
  2141. vertex_array_attrib_binding(struct gl_context *ctx,
  2142.                             struct gl_vertex_array_object *vao,
  2143.                             GLuint attribIndex, GLuint bindingIndex,
  2144.                             const char *func)
  2145. {
  2146.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  2147.  
  2148.    /* The ARB_vertex_attrib_binding spec says:
  2149.     *
  2150.     *    "<attribindex> must be less than the value of MAX_VERTEX_ATTRIBS and
  2151.     *     <bindingindex> must be less than the value of
  2152.     *     MAX_VERTEX_ATTRIB_BINDINGS, otherwise the error INVALID_VALUE
  2153.     *     is generated."
  2154.     */
  2155.    if (attribIndex >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
  2156.       _mesa_error(ctx, GL_INVALID_VALUE,
  2157.                   "%s(attribindex=%u >= "
  2158.                   "GL_MAX_VERTEX_ATTRIBS)",
  2159.                   func, attribIndex);
  2160.       return;
  2161.    }
  2162.  
  2163.    if (bindingIndex >= ctx->Const.MaxVertexAttribBindings) {
  2164.       _mesa_error(ctx, GL_INVALID_VALUE,
  2165.                   "%s(bindingindex=%u >= "
  2166.                   "GL_MAX_VERTEX_ATTRIB_BINDINGS)",
  2167.                   func, bindingIndex);
  2168.       return;
  2169.    }
  2170.  
  2171.    assert(VERT_ATTRIB_GENERIC(attribIndex) < ARRAY_SIZE(vao->VertexAttrib));
  2172.  
  2173.    vertex_attrib_binding(ctx, vao,
  2174.                          VERT_ATTRIB_GENERIC(attribIndex),
  2175.                          VERT_ATTRIB_GENERIC(bindingIndex));
  2176. }
  2177.  
  2178.  
  2179. void GLAPIENTRY
  2180. _mesa_VertexAttribBinding(GLuint attribIndex, GLuint bindingIndex)
  2181. {
  2182.    GET_CURRENT_CONTEXT(ctx);
  2183.  
  2184.    /* The ARB_vertex_attrib_binding spec says:
  2185.     *
  2186.     *    "An INVALID_OPERATION error is generated if no vertex array object
  2187.     *     is bound."
  2188.     */
  2189.    if (ctx->API == API_OPENGL_CORE &&
  2190.        ctx->Array.VAO == ctx->Array.DefaultVAO) {
  2191.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2192.                   "glVertexAttribBinding(No array object bound)");
  2193.       return;
  2194.    }
  2195.  
  2196.    vertex_array_attrib_binding(ctx, ctx->Array.VAO,
  2197.                                attribIndex, bindingIndex,
  2198.                                "glVertexAttribBinding");
  2199. }
  2200.  
  2201.  
  2202. void GLAPIENTRY
  2203. _mesa_VertexArrayAttribBinding(GLuint vaobj, GLuint attribIndex, GLuint bindingIndex)
  2204. {
  2205.    GET_CURRENT_CONTEXT(ctx);
  2206.    struct gl_vertex_array_object *vao;
  2207.  
  2208.    if (!ctx->Extensions.ARB_direct_state_access) {
  2209.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2210.                    "glVertexArrayAttribBinding(GL_ARB_direct_state_access "
  2211.                    "is not supported");
  2212.       return;
  2213.    }
  2214.  
  2215.    /* The ARB_direct_state_access specification says:
  2216.     *
  2217.     *   "An INVALID_OPERATION error is generated by VertexArrayAttribBinding
  2218.     *    if <vaobj> is not [compatibility profile: zero or] the name of an
  2219.     *    existing vertex array object."
  2220.     */
  2221.    vao = _mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayAttribBinding");
  2222.    if (!vao)
  2223.       return;
  2224.  
  2225.    vertex_array_attrib_binding(ctx, vao, attribIndex, bindingIndex,
  2226.                                "glVertexArrayAttribBinding");
  2227. }
  2228.  
  2229.  
  2230. static void
  2231. vertex_array_binding_divisor(struct gl_context *ctx,
  2232.                              struct gl_vertex_array_object *vao,
  2233.                              GLuint bindingIndex, GLuint divisor,
  2234.                              const char *func)
  2235. {
  2236.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  2237.  
  2238.    if (!ctx->Extensions.ARB_instanced_arrays) {
  2239.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s()", func);
  2240.       return;
  2241.    }
  2242.  
  2243.    /* The ARB_vertex_attrib_binding spec says:
  2244.     *
  2245.     *    "An INVALID_VALUE error is generated if <bindingindex> is greater
  2246.     *     than or equal to the value of MAX_VERTEX_ATTRIB_BINDINGS."
  2247.     */
  2248.    if (bindingIndex >= ctx->Const.MaxVertexAttribBindings) {
  2249.       _mesa_error(ctx, GL_INVALID_VALUE,
  2250.                   "%s(bindingindex=%u > "
  2251.                   "GL_MAX_VERTEX_ATTRIB_BINDINGS)",
  2252.                   func, bindingIndex);
  2253.       return;
  2254.    }
  2255.  
  2256.    vertex_binding_divisor(ctx, vao, VERT_ATTRIB_GENERIC(bindingIndex), divisor);
  2257. }
  2258.  
  2259.  
  2260. void GLAPIENTRY
  2261. _mesa_VertexBindingDivisor(GLuint bindingIndex, GLuint divisor)
  2262. {
  2263.    GET_CURRENT_CONTEXT(ctx);
  2264.  
  2265.    /* The ARB_vertex_attrib_binding spec says:
  2266.     *
  2267.     *    "An INVALID_OPERATION error is generated if no vertex array object
  2268.     *     is bound."
  2269.     */
  2270.    if (ctx->API == API_OPENGL_CORE &&
  2271.        ctx->Array.VAO == ctx->Array.DefaultVAO) {
  2272.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2273.                   "glVertexBindingDivisor(No array object bound)");
  2274.       return;
  2275.    }
  2276.  
  2277.    vertex_array_binding_divisor(ctx, ctx->Array.VAO,
  2278.                                 bindingIndex, divisor,
  2279.                                 "glVertexBindingDivisor");
  2280. }
  2281.  
  2282.  
  2283. void GLAPIENTRY
  2284. _mesa_VertexArrayBindingDivisor(GLuint vaobj, GLuint bindingIndex, GLuint divisor)
  2285. {
  2286.    struct gl_vertex_array_object *vao;
  2287.    GET_CURRENT_CONTEXT(ctx);
  2288.  
  2289.    if (!ctx->Extensions.ARB_direct_state_access) {
  2290.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2291.                    "glVertexArrayBindingDivisor(GL_ARB_direct_state_access "
  2292.                    "is not supported");
  2293.       return;
  2294.    }
  2295.  
  2296.    /* The ARB_direct_state_access specification says:
  2297.     *
  2298.     *   "An INVALID_OPERATION error is generated by VertexArrayBindingDivisor
  2299.     *    if <vaobj> is not [compatibility profile: zero or] the name of an
  2300.     *    existing vertex array object."
  2301.     */
  2302.    vao = _mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayBindingDivisor");
  2303.    if (!vao)
  2304.        return;
  2305.  
  2306.    vertex_array_binding_divisor(ctx, vao, bindingIndex, divisor,
  2307.                                 "glVertexArrayBindingDivisor");
  2308. }
  2309.  
  2310.  
  2311. /**
  2312.  * Copy one client vertex array to another.
  2313.  */
  2314. void
  2315. _mesa_copy_client_array(struct gl_context *ctx,
  2316.                         struct gl_client_array *dst,
  2317.                         struct gl_client_array *src)
  2318. {
  2319.    dst->Size = src->Size;
  2320.    dst->Type = src->Type;
  2321.    dst->Format = src->Format;
  2322.    dst->Stride = src->Stride;
  2323.    dst->StrideB = src->StrideB;
  2324.    dst->Ptr = src->Ptr;
  2325.    dst->Enabled = src->Enabled;
  2326.    dst->Normalized = src->Normalized;
  2327.    dst->Integer = src->Integer;
  2328.    dst->Doubles = src->Doubles;
  2329.    dst->InstanceDivisor = src->InstanceDivisor;
  2330.    dst->_ElementSize = src->_ElementSize;
  2331.    _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
  2332. }
  2333.  
  2334. void
  2335. _mesa_copy_vertex_attrib_array(struct gl_context *ctx,
  2336.                                struct gl_vertex_attrib_array *dst,
  2337.                                const struct gl_vertex_attrib_array *src)
  2338. {
  2339.    dst->Size           = src->Size;
  2340.    dst->Type           = src->Type;
  2341.    dst->Format         = src->Format;
  2342.    dst->VertexBinding  = src->VertexBinding;
  2343.    dst->RelativeOffset = src->RelativeOffset;
  2344.    dst->Format         = src->Format;
  2345.    dst->Integer        = src->Integer;
  2346.    dst->Doubles        = src->Doubles;
  2347.    dst->Normalized     = src->Normalized;
  2348.    dst->Ptr            = src->Ptr;
  2349.    dst->Enabled        = src->Enabled;
  2350.    dst->_ElementSize   = src->_ElementSize;
  2351. }
  2352.  
  2353. void
  2354. _mesa_copy_vertex_buffer_binding(struct gl_context *ctx,
  2355.                                  struct gl_vertex_buffer_binding *dst,
  2356.                                  const struct gl_vertex_buffer_binding *src)
  2357. {
  2358.    dst->Offset          = src->Offset;
  2359.    dst->Stride          = src->Stride;
  2360.    dst->InstanceDivisor = src->InstanceDivisor;
  2361.    dst->_BoundArrays    = src->_BoundArrays;
  2362.  
  2363.    _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
  2364. }
  2365.  
  2366. /**
  2367.  * Print vertex array's fields.
  2368.  */
  2369. static void
  2370. print_array(const char *name, GLint index, const struct gl_client_array *array)
  2371. {
  2372.    if (index >= 0)
  2373.       fprintf(stderr, "  %s[%d]: ", name, index);
  2374.    else
  2375.       fprintf(stderr, "  %s: ", name);
  2376.    fprintf(stderr, "Ptr=%p, Type=0x%x, Size=%d, ElemSize=%u, Stride=%d, Buffer=%u(Size %lu)\n",
  2377.           array->Ptr, array->Type, array->Size,
  2378.           array->_ElementSize, array->StrideB,
  2379.           array->BufferObj->Name, (unsigned long) array->BufferObj->Size);
  2380. }
  2381.  
  2382.  
  2383. /**
  2384.  * Print current vertex object/array info.  For debug.
  2385.  */
  2386. void
  2387. _mesa_print_arrays(struct gl_context *ctx)
  2388. {
  2389.    struct gl_vertex_array_object *vao = ctx->Array.VAO;
  2390.    GLuint i;
  2391.  
  2392.    printf("Array Object %u\n", vao->Name);
  2393.    if (vao->_VertexAttrib[VERT_ATTRIB_POS].Enabled)
  2394.       print_array("Vertex", -1, &vao->_VertexAttrib[VERT_ATTRIB_POS]);
  2395.    if (vao->_VertexAttrib[VERT_ATTRIB_NORMAL].Enabled)
  2396.       print_array("Normal", -1, &vao->_VertexAttrib[VERT_ATTRIB_NORMAL]);
  2397.    if (vao->_VertexAttrib[VERT_ATTRIB_COLOR0].Enabled)
  2398.       print_array("Color", -1, &vao->_VertexAttrib[VERT_ATTRIB_COLOR0]);
  2399.    for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++)
  2400.       if (vao->_VertexAttrib[VERT_ATTRIB_TEX(i)].Enabled)
  2401.          print_array("TexCoord", i, &vao->_VertexAttrib[VERT_ATTRIB_TEX(i)]);
  2402.    for (i = 0; i < VERT_ATTRIB_GENERIC_MAX; i++)
  2403.       if (vao->_VertexAttrib[VERT_ATTRIB_GENERIC(i)].Enabled)
  2404.          print_array("Attrib", i, &vao->_VertexAttrib[VERT_ATTRIB_GENERIC(i)]);
  2405. }
  2406.  
  2407.  
  2408. /**
  2409.  * Initialize vertex array state for given context.
  2410.  */
  2411. void
  2412. _mesa_init_varray(struct gl_context *ctx)
  2413. {
  2414.    ctx->Array.DefaultVAO = ctx->Driver.NewArrayObject(ctx, 0);
  2415.    _mesa_reference_vao(ctx, &ctx->Array.VAO, ctx->Array.DefaultVAO);
  2416.    ctx->Array.ActiveTexture = 0;   /* GL_ARB_multitexture */
  2417.  
  2418.    ctx->Array.Objects = _mesa_NewHashTable();
  2419. }
  2420.  
  2421.  
  2422. /**
  2423.  * Callback for deleting an array object.  Called by _mesa_HashDeleteAll().
  2424.  */
  2425. static void
  2426. delete_arrayobj_cb(GLuint id, void *data, void *userData)
  2427. {
  2428.    struct gl_vertex_array_object *vao = (struct gl_vertex_array_object *) data;
  2429.    struct gl_context *ctx = (struct gl_context *) userData;
  2430.    _mesa_delete_vao(ctx, vao);
  2431. }
  2432.  
  2433.  
  2434. /**
  2435.  * Free vertex array state for given context.
  2436.  */
  2437. void
  2438. _mesa_free_varray_data(struct gl_context *ctx)
  2439. {
  2440.    _mesa_HashDeleteAll(ctx->Array.Objects, delete_arrayobj_cb, ctx);
  2441.    _mesa_DeleteHashTable(ctx->Array.Objects);
  2442. }
  2443.