mesa: remove array size so the static assert can work
[mesa.git] / src / mesa / main / varray.c
index bcde65adc70107948b6a06f37ffdae56d290b15e..522f5a7042c735f1d2a69e58f6d2d41069a8e569 100644 (file)
 #define HALF_BIT             0x80
 #define FLOAT_BIT            0x100
 #define DOUBLE_BIT           0x200
-#define FIXED_BIT            0x400
-
+#define FIXED_ES_BIT         0x400
+#define FIXED_GL_BIT         0x800
+#define UNSIGNED_INT_2_10_10_10_REV_BIT 0x1000
+#define INT_2_10_10_10_REV_BIT 0x2000
 
 
 /** Convert GL datatype enum into a <type>_BIT value seen above */
@@ -90,7 +92,11 @@ type_to_bit(const struct gl_context *ctx, GLenum type)
    case GL_DOUBLE:
       return DOUBLE_BIT;
    case GL_FIXED:
-      return FIXED_BIT;
+      return _mesa_is_desktop_gl(ctx) ? FIXED_GL_BIT : FIXED_ES_BIT;
+   case GL_UNSIGNED_INT_2_10_10_10_REV:
+      return UNSIGNED_INT_2_10_10_10_REV_BIT;
+   case GL_INT_2_10_10_10_REV:
+      return INT_2_10_10_10_REV_BIT;
    default:
       return 0;
    }
@@ -102,8 +108,7 @@ type_to_bit(const struct gl_context *ctx, GLenum type)
  * functions.
  *
  * \param func  name of calling function used for error reporting
- * \param array  the array to update
- * \param dirtyBit  which bit to set in ctx->Array.NewState for this array
+ * \param attrib  the attribute array index to update
  * \param legalTypes  bitmask of *_BIT above indicating legal datatypes
  * \param sizeMin  min allowable size value
  * \param sizeMax  max allowable size value (may also be BGRA_OR_4)
@@ -117,20 +122,65 @@ type_to_bit(const struct gl_context *ctx, GLenum type)
 static void
 update_array(struct gl_context *ctx,
              const char *func,
-             struct gl_client_array *array,
-             GLbitfield dirtyBit, GLbitfield legalTypesMask,
+             GLuint attrib, GLbitfield legalTypesMask,
              GLint sizeMin, GLint sizeMax,
              GLint size, GLenum type, GLsizei stride,
              GLboolean normalized, GLboolean integer,
              const GLvoid *ptr)
 {
+   struct gl_client_array *array;
    GLbitfield typeBit;
    GLsizei elementSize;
    GLenum format = GL_RGBA;
 
-   if (ctx->API != API_OPENGLES && ctx->API != API_OPENGLES2) {
-      /* fixed point arrays / data is only allowed with OpenGL ES 1.x/2.0 */
-      legalTypesMask &= ~FIXED_BIT;
+   /* Page 407 (page 423 of the PDF) of the OpenGL 3.0 spec says:
+    *
+    *     "Client vertex arrays - all vertex array attribute pointers must
+    *     refer to buffer objects (section 2.9.2). The default vertex array
+    *     object (the name zero) is also deprecated. Calling
+    *     VertexAttribPointer when no buffer object or no vertex array object
+    *     is bound will generate an INVALID_OPERATION error..."
+    *
+    * The check for VBOs is handled below.
+    */
+   if (ctx->API == API_OPENGL_CORE
+       && (ctx->Array.ArrayObj == ctx->Array.DefaultArrayObj)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no array object bound)",
+                  func);
+      return;
+   }
+
+   if (_mesa_is_gles(ctx)) {
+      /* Once Mesa gets support for GL_OES_vertex_half_float this mask will
+       * change.  Adding support for this extension isn't quite as trivial as
+       * we'd like because ES uses a different enum value for GL_HALF_FLOAT.
+       */
+      legalTypesMask &= ~(FIXED_GL_BIT | HALF_BIT | DOUBLE_BIT);
+
+      /* GL_INT and GL_UNSIGNED_INT data is not allowed in OpenGL ES until
+       * 3.0.  The 2_10_10_10 types are added in OpenGL ES 3.0 or
+       * GL_OES_vertex_type_10_10_10_2.
+       */
+      if (ctx->Version < 30) {
+         legalTypesMask &= ~(UNSIGNED_INT_BIT
+                             | INT_BIT
+                             | UNSIGNED_INT_2_10_10_10_REV_BIT
+                             | INT_2_10_10_10_REV_BIT);
+      }
+
+      /* BGRA ordering is not supported in ES contexts.
+       */
+      if (sizeMax == BGRA_OR_4)
+         sizeMax = 4;
+   } else {
+      legalTypesMask &= ~FIXED_ES_BIT;
+
+      if (!ctx->Extensions.ARB_ES2_compatibility)
+         legalTypesMask &= ~FIXED_GL_BIT;
+
+      if (!ctx->Extensions.ARB_vertex_type_2_10_10_10_rev)
+         legalTypesMask &= ~(UNSIGNED_INT_2_10_10_10_REV_BIT |
+                             INT_2_10_10_10_REV_BIT);
    }
 
    typeBit = type_to_bit(ctx, type);
@@ -147,7 +197,17 @@ update_array(struct gl_context *ctx,
    if (ctx->Extensions.EXT_vertex_array_bgra &&
        sizeMax == BGRA_OR_4 &&
        size == GL_BGRA) {
-      if (type != GL_UNSIGNED_BYTE) {
+      GLboolean bgra_error = GL_FALSE;
+
+      if (ctx->Extensions.ARB_vertex_type_2_10_10_10_rev) {
+         if (type != GL_UNSIGNED_INT_2_10_10_10_REV &&
+             type != GL_INT_2_10_10_10_REV &&
+             type != GL_UNSIGNED_BYTE)
+            bgra_error = GL_TRUE;
+      } else if (type != GL_UNSIGNED_BYTE)
+         bgra_error = GL_TRUE;
+
+      if (bgra_error) {
          _mesa_error(ctx, GL_INVALID_VALUE, "%s(GL_BGRA/GLubyte)", func);
          return;
       }
@@ -159,6 +219,13 @@ update_array(struct gl_context *ctx,
       return;
    }
 
+   if (ctx->Extensions.ARB_vertex_type_2_10_10_10_rev &&
+       (type == GL_UNSIGNED_INT_2_10_10_10_REV ||
+        type == GL_INT_2_10_10_10_REV) && size != 4) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(size=%d)", func, size);
+      return;
+   }
+
    ASSERT(size <= 4);
 
    if (stride < 0) {
@@ -166,23 +233,34 @@ update_array(struct gl_context *ctx,
       return;
    }
 
-   if (ctx->Array.ArrayObj->VBOonly &&
-       ctx->Array.ArrayBufferObj->Name == 0) {
-      /* GL_ARB_vertex_array_object requires that all arrays reside in VBOs.
-       * Generate GL_INVALID_OPERATION if that's not true.
-       */
+   /* Page 29 (page 44 of the PDF) of the OpenGL 3.3 spec says:
+    *
+    *     "An INVALID_OPERATION error is generated under any of the following
+    *     conditions:
+    *
+    *     ...
+    *
+    *     * any of the *Pointer commands specifying the location and
+    *       organization of vertex array data are called while zero is bound
+    *       to the ARRAY_BUFFER buffer object binding point (see section
+    *       2.9.6), and the pointer argument is not NULL."
+    */
+   if (ptr != NULL && ctx->Array.ArrayObj->ARBsemantics &&
+       !_mesa_is_bufferobj(ctx->Array.ArrayBufferObj)) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-VBO array)", func);
       return;
    }
 
    elementSize = _mesa_sizeof_type(type) * size;
 
+   array = &ctx->Array.ArrayObj->VertexAttrib[attrib];
    array->Size = size;
    array->Type = type;
    array->Format = format;
    array->Stride = stride;
    array->StrideB = stride ? stride : elementSize;
    array->Normalized = normalized;
+   array->Integer = integer;
    array->Ptr = (const GLubyte *) ptr;
    array->_ElementSize = elementSize;
 
@@ -190,23 +268,23 @@ update_array(struct gl_context *ctx,
                                  ctx->Array.ArrayBufferObj);
 
    ctx->NewState |= _NEW_ARRAY;
-   ctx->Array.NewState |= dirtyBit;
+   ctx->Array.ArrayObj->NewArrays |= VERT_BIT(attrib);
 }
 
 
 void GLAPIENTRY
 _mesa_VertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr)
 {
-   GLbitfield legalTypes = (SHORT_BIT | INT_BIT | FLOAT_BIT |
-                            DOUBLE_BIT | HALF_BIT | FIXED_BIT);
    GET_CURRENT_CONTEXT(ctx);
+   GLbitfield legalTypes = (ctx->API == API_OPENGLES)
+      ? (BYTE_BIT | SHORT_BIT | FLOAT_BIT | FIXED_ES_BIT)
+      : (SHORT_BIT | INT_BIT | FLOAT_BIT |
+         DOUBLE_BIT | HALF_BIT |
+         UNSIGNED_INT_2_10_10_10_REV_BIT |
+         INT_2_10_10_10_REV_BIT);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   if (ctx->API == API_OPENGLES)
-      legalTypes |= BYTE_BIT;
-
-   update_array(ctx, "glVertexPointer",
-                &ctx->Array.ArrayObj->Vertex, _NEW_ARRAY_VERTEX,
+   update_array(ctx, "glVertexPointer", VERT_ATTRIB_POS,
                 legalTypes, 2, 4,
                 size, type, stride, GL_FALSE, GL_FALSE, ptr);
 }
@@ -215,14 +293,16 @@ _mesa_VertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr)
 void GLAPIENTRY
 _mesa_NormalPointer(GLenum type, GLsizei stride, const GLvoid *ptr )
 {
-   const GLbitfield legalTypes = (BYTE_BIT | SHORT_BIT | INT_BIT |
-                                  HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
-                                  FIXED_BIT);
    GET_CURRENT_CONTEXT(ctx);
+   const GLbitfield legalTypes = (ctx->API == API_OPENGLES)
+      ? (BYTE_BIT | SHORT_BIT | FLOAT_BIT | FIXED_ES_BIT)
+      : (BYTE_BIT | SHORT_BIT | INT_BIT |
+         HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
+         UNSIGNED_INT_2_10_10_10_REV_BIT |
+         INT_2_10_10_10_REV_BIT);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   update_array(ctx, "glNormalPointer",
-                &ctx->Array.ArrayObj->Normal, _NEW_ARRAY_NORMAL,
+   update_array(ctx, "glNormalPointer", VERT_ATTRIB_NORMAL,
                 legalTypes, 3, 3,
                 3, type, stride, GL_TRUE, GL_FALSE, ptr);
 }
@@ -231,17 +311,20 @@ _mesa_NormalPointer(GLenum type, GLsizei stride, const GLvoid *ptr )
 void GLAPIENTRY
 _mesa_ColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr)
 {
-   const GLbitfield legalTypes = (BYTE_BIT | UNSIGNED_BYTE_BIT |
-                                  SHORT_BIT | UNSIGNED_SHORT_BIT |
-                                  INT_BIT | UNSIGNED_INT_BIT |
-                                  HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
-                                  FIXED_BIT);
    GET_CURRENT_CONTEXT(ctx);
+   const GLbitfield legalTypes = (ctx->API == API_OPENGLES)
+      ? (UNSIGNED_BYTE_BIT | HALF_BIT | FLOAT_BIT | FIXED_ES_BIT)
+      : (BYTE_BIT | UNSIGNED_BYTE_BIT |
+         SHORT_BIT | UNSIGNED_SHORT_BIT |
+         INT_BIT | UNSIGNED_INT_BIT |
+         HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
+         UNSIGNED_INT_2_10_10_10_REV_BIT |
+         INT_2_10_10_10_REV_BIT);
+   const GLint sizeMin = (ctx->API == API_OPENGLES) ? 4 : 3;
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   update_array(ctx, "glColorPointer",
-                &ctx->Array.ArrayObj->Color, _NEW_ARRAY_COLOR0,
-                legalTypes, 3, BGRA_OR_4,
+   update_array(ctx, "glColorPointer", VERT_ATTRIB_COLOR0,
+                legalTypes, sizeMin, BGRA_OR_4,
                 size, type, stride, GL_TRUE, GL_FALSE, ptr);
 }
 
@@ -253,8 +336,7 @@ _mesa_FogCoordPointerEXT(GLenum type, GLsizei stride, const GLvoid *ptr)
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   update_array(ctx, "glFogCoordPointer",
-                &ctx->Array.ArrayObj->FogCoord, _NEW_ARRAY_FOGCOORD,
+   update_array(ctx, "glFogCoordPointer", VERT_ATTRIB_FOG,
                 legalTypes, 1, 1,
                 1, type, stride, GL_FALSE, GL_FALSE, ptr);
 }
@@ -268,8 +350,7 @@ _mesa_IndexPointer(GLenum type, GLsizei stride, const GLvoid *ptr)
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   update_array(ctx, "glIndexPointer",
-                &ctx->Array.ArrayObj->Index, _NEW_ARRAY_INDEX,
+   update_array(ctx, "glIndexPointer", VERT_ATTRIB_COLOR_INDEX,
                 legalTypes, 1, 1,
                 1, type, stride, GL_FALSE, GL_FALSE, ptr);
 }
@@ -282,12 +363,13 @@ _mesa_SecondaryColorPointerEXT(GLint size, GLenum type,
    const GLbitfield legalTypes = (BYTE_BIT | UNSIGNED_BYTE_BIT |
                                   SHORT_BIT | UNSIGNED_SHORT_BIT |
                                   INT_BIT | UNSIGNED_INT_BIT |
-                                  HALF_BIT | FLOAT_BIT | DOUBLE_BIT);
+                                  HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
+                                  UNSIGNED_INT_2_10_10_10_REV_BIT |
+                                  INT_2_10_10_10_REV_BIT);
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   update_array(ctx, "glSecondaryColorPointer",
-                &ctx->Array.ArrayObj->SecondaryColor, _NEW_ARRAY_COLOR1,
+   update_array(ctx, "glSecondaryColorPointer", VERT_ATTRIB_COLOR1,
                 legalTypes, 3, BGRA_OR_4,
                 size, type, stride, GL_TRUE, GL_FALSE, ptr);
 }
@@ -297,22 +379,19 @@ void GLAPIENTRY
 _mesa_TexCoordPointer(GLint size, GLenum type, GLsizei stride,
                       const GLvoid *ptr)
 {
-   GLbitfield legalTypes = (SHORT_BIT | INT_BIT |
-                            HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
-                            FIXED_BIT);
    GET_CURRENT_CONTEXT(ctx);
+   GLbitfield legalTypes = (ctx->API == API_OPENGLES)
+      ? (BYTE_BIT | SHORT_BIT | FLOAT_BIT | FIXED_ES_BIT)
+      : (SHORT_BIT | INT_BIT |
+         HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
+         UNSIGNED_INT_2_10_10_10_REV_BIT |
+         INT_2_10_10_10_REV_BIT);
+   const GLint sizeMin = (ctx->API == API_OPENGLES) ? 2 : 1;
    const GLuint unit = ctx->Array.ActiveTexture;
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   if (ctx->API == API_OPENGLES)
-      legalTypes |= BYTE_BIT;
-
-   ASSERT(unit < Elements(ctx->Array.ArrayObj->TexCoord));
-
-   update_array(ctx, "glTexCoordPointer",
-                &ctx->Array.ArrayObj->TexCoord[unit],
-                _NEW_ARRAY_TEXCOORD(unit),
-                legalTypes, 1, 4,
+   update_array(ctx, "glTexCoordPointer", VERT_ATTRIB_TEX(unit),
+                legalTypes, sizeMin, 4,
                 size, type, stride, GL_FALSE, GL_FALSE,
                 ptr);
 }
@@ -327,8 +406,7 @@ _mesa_EdgeFlagPointer(GLsizei stride, const GLvoid *ptr)
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   update_array(ctx, "glEdgeFlagPointer",
-                &ctx->Array.ArrayObj->EdgeFlag, _NEW_ARRAY_EDGEFLAG,
+   update_array(ctx, "glEdgeFlagPointer", VERT_ATTRIB_EDGEFLAG,
                 legalTypes, 1, 1,
                 1, GL_UNSIGNED_BYTE, stride, GL_FALSE, integer, ptr);
 }
@@ -337,7 +415,7 @@ _mesa_EdgeFlagPointer(GLsizei stride, const GLvoid *ptr)
 void GLAPIENTRY
 _mesa_PointSizePointer(GLenum type, GLsizei stride, const GLvoid *ptr)
 {
-   const GLbitfield legalTypes = (FLOAT_BIT | FIXED_BIT);
+   const GLbitfield legalTypes = (FLOAT_BIT | FIXED_ES_BIT);
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
@@ -347,50 +425,12 @@ _mesa_PointSizePointer(GLenum type, GLsizei stride, const GLvoid *ptr)
       return;
    }
       
-   update_array(ctx, "glPointSizePointer",
-                &ctx->Array.ArrayObj->PointSize, _NEW_ARRAY_POINT_SIZE,
+   update_array(ctx, "glPointSizePointer", VERT_ATTRIB_POINT_SIZE,
                 legalTypes, 1, 1,
                 1, type, stride, GL_FALSE, GL_FALSE, ptr);
 }
 
 
-#if FEATURE_NV_vertex_program
-/**
- * Set a vertex attribute array.
- * Note that these arrays DO alias the conventional GL vertex arrays
- * (position, normal, color, fog, texcoord, etc).
- * The generic attribute slots at #16 and above are not touched.
- */
-void GLAPIENTRY
-_mesa_VertexAttribPointerNV(GLuint index, GLint size, GLenum type,
-                            GLsizei stride, const GLvoid *ptr)
-{
-   const GLbitfield legalTypes = (UNSIGNED_BYTE_BIT | SHORT_BIT |
-                                  FLOAT_BIT | DOUBLE_BIT);
-   GLboolean normalized = GL_FALSE;
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
-
-   if (index >= MAX_NV_VERTEX_PROGRAM_INPUTS) {
-      _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribPointerNV(index)");
-      return;
-   }
-
-   if (type == GL_UNSIGNED_BYTE && size != 4) {
-      _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribPointerNV(size!=4)");
-      return;
-   }
-
-   update_array(ctx, "glVertexAttribPointerNV",
-                &ctx->Array.ArrayObj->VertexAttrib[index],
-                _NEW_ARRAY_ATTRIB(index),
-                legalTypes, 1, BGRA_OR_4,
-                size, type, stride, normalized, GL_FALSE, ptr);
-}
-#endif
-
-
-#if FEATURE_ARB_vertex_program
 /**
  * Set a generic vertex attribute array.
  * Note that these arrays DO NOT alias the conventional GL vertex arrays
@@ -405,7 +445,9 @@ _mesa_VertexAttribPointerARB(GLuint index, GLint size, GLenum type,
                                   SHORT_BIT | UNSIGNED_SHORT_BIT |
                                   INT_BIT | UNSIGNED_INT_BIT |
                                   HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
-                                  FIXED_BIT);
+                                  FIXED_ES_BIT | FIXED_GL_BIT |
+                                  UNSIGNED_INT_2_10_10_10_REV_BIT |
+                                  INT_2_10_10_10_REV_BIT);
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
@@ -414,13 +456,10 @@ _mesa_VertexAttribPointerARB(GLuint index, GLint size, GLenum type,
       return;
    }
 
-   update_array(ctx, "glVertexAttribPointer",
-                &ctx->Array.ArrayObj->VertexAttrib[index],
-                _NEW_ARRAY_ATTRIB(index),
+   update_array(ctx, "glVertexAttribPointer", VERT_ATTRIB_GENERIC(index),
                 legalTypes, 1, BGRA_OR_4,
                 size, type, stride, normalized, GL_FALSE, ptr);
 }
-#endif
 
 
 /**
@@ -446,9 +485,7 @@ _mesa_VertexAttribIPointer(GLuint index, GLint size, GLenum type,
       return;
    }
 
-   update_array(ctx, "glVertexAttribIPointer",
-                &ctx->Array.ArrayObj->VertexAttrib[index],
-                _NEW_ARRAY_ATTRIB(index),
+   update_array(ctx, "glVertexAttribIPointer", VERT_ATTRIB_GENERIC(index),
                 legalTypes, 1, 4,
                 size, type, stride, normalized, integer, ptr);
 }
@@ -458,6 +495,7 @@ _mesa_VertexAttribIPointer(GLuint index, GLint size, GLenum type,
 void GLAPIENTRY
 _mesa_EnableVertexAttribArrayARB(GLuint index)
 {
+   struct gl_array_object *arrayObj;
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
@@ -467,33 +505,44 @@ _mesa_EnableVertexAttribArrayARB(GLuint index)
       return;
    }
 
-   ASSERT(index < Elements(ctx->Array.ArrayObj->VertexAttrib));
+   arrayObj = ctx->Array.ArrayObj;
+
+   ASSERT(VERT_ATTRIB_GENERIC(index) < Elements(arrayObj->VertexAttrib));
 
-   FLUSH_VERTICES(ctx, _NEW_ARRAY);
-   ctx->Array.ArrayObj->VertexAttrib[index].Enabled = GL_TRUE;
-   ctx->Array.ArrayObj->_Enabled |= _NEW_ARRAY_ATTRIB(index);
-   ctx->Array.NewState |= _NEW_ARRAY_ATTRIB(index);
+   if (!arrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled) {
+      /* was disabled, now being enabled */
+      FLUSH_VERTICES(ctx, _NEW_ARRAY);
+      arrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled = GL_TRUE;
+      arrayObj->_Enabled |= VERT_BIT_GENERIC(index);
+      arrayObj->NewArrays |= VERT_BIT_GENERIC(index);
+   }
 }
 
 
 void GLAPIENTRY
 _mesa_DisableVertexAttribArrayARB(GLuint index)
 {
+   struct gl_array_object *arrayObj;
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (index >= ctx->Const.VertexProgram.MaxAttribs) {
       _mesa_error(ctx, GL_INVALID_VALUE,
-                  "glEnableVertexAttribArrayARB(index)");
+                  "glDisableVertexAttribArrayARB(index)");
       return;
    }
 
-   ASSERT(index < Elements(ctx->Array.ArrayObj->VertexAttrib));
+   arrayObj = ctx->Array.ArrayObj;
+
+   ASSERT(VERT_ATTRIB_GENERIC(index) < Elements(arrayObj->VertexAttrib));
 
-   FLUSH_VERTICES(ctx, _NEW_ARRAY);
-   ctx->Array.ArrayObj->VertexAttrib[index].Enabled = GL_FALSE;
-   ctx->Array.ArrayObj->_Enabled &= ~_NEW_ARRAY_ATTRIB(index);
-   ctx->Array.NewState |= _NEW_ARRAY_ATTRIB(index);
+   if (arrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled) {
+      /* was enabled, now being disabled */
+      FLUSH_VERTICES(ctx, _NEW_ARRAY);
+      arrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled = GL_FALSE;
+      arrayObj->_Enabled &= ~VERT_BIT_GENERIC(index);
+      arrayObj->NewArrays |= VERT_BIT_GENERIC(index);
+   }
 }
 
 
@@ -508,14 +557,14 @@ get_vertex_array_attrib(struct gl_context *ctx, GLuint index, GLenum pname,
 {
    const struct gl_client_array *array;
 
-   if (index >= MAX_VERTEX_GENERIC_ATTRIBS) {
+   if (index >= ctx->Const.VertexProgram.MaxAttribs) {
       _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%u)", caller, index);
       return 0;
    }
 
-   ASSERT(index < Elements(ctx->Array.ArrayObj->VertexAttrib));
+   ASSERT(VERT_ATTRIB_GENERIC(index) < Elements(ctx->Array.ArrayObj->VertexAttrib));
 
-   array = &ctx->Array.ArrayObj->VertexAttrib[index];
+   array = &ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(index)];
 
    switch (pname) {
    case GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB:
@@ -531,17 +580,55 @@ get_vertex_array_attrib(struct gl_context *ctx, GLuint index, GLenum pname,
    case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB:
       return array->BufferObj->Name;
    case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
-      if (ctx->Extensions.EXT_gpu_shader4) {
+      if ((_mesa_is_desktop_gl(ctx)
+           && (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4))
+          || _mesa_is_gles3(ctx)) {
          return array->Integer;
       }
-      /* fall-through */
+      goto error;
+   case GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ARB:
+      if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_instanced_arrays)
+          || _mesa_is_gles3(ctx)) {
+         return array->InstanceDivisor;
+      }
+      goto error;
    default:
-      _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", caller, pname);
-      return 0;
+      ; /* fall-through */
    }
+
+error:
+   _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", caller, pname);
+   return 0;
 }
 
 
+static const GLfloat *
+get_current_attrib(struct gl_context *ctx, GLuint index, const char *function)
+{
+   if (index == 0) {
+      /* In OpenGL 3.1 attribute 0 becomes non-magic, just like in OpenGL ES
+       * 2.0.  Note that we cannot just check for API_OPENGL_CORE here because
+       * that will erroneously allow this usage in a 3.0 forward-compatible
+       * context too.
+       */
+      if ((ctx->API != API_OPENGL_CORE || ctx->Version < 31)
+          && ctx->API != API_OPENGLES2) {
+        _mesa_error(ctx, GL_INVALID_OPERATION, "%s(index==0)", function);
+        return NULL;
+      }
+   }
+   else if (index >= ctx->Const.VertexProgram.MaxAttribs) {
+      _mesa_error(ctx, GL_INVALID_VALUE,
+                 "%s(index>=GL_MAX_VERTEX_ATTRIBS)", function);
+      return NULL;
+   }
+
+   ASSERT(VERT_ATTRIB_GENERIC(index) < Elements(ctx->Array.ArrayObj->VertexAttrib));
+
+   FLUSH_CURRENT(ctx, 0);
+   return ctx->Current.Attrib[VERT_ATTRIB_GENERIC(index)];
+}
+
 void GLAPIENTRY
 _mesa_GetVertexAttribfvARB(GLuint index, GLenum pname, GLfloat *params)
 {
@@ -549,13 +636,8 @@ _mesa_GetVertexAttribfvARB(GLuint index, GLenum pname, GLfloat *params)
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
-      if (index == 0) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetVertexAttribfv(index==0)");
-      }
-      else {
-         const GLfloat *v = ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + index];
-         FLUSH_CURRENT(ctx, 0);
+      const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribfv");
+      if (v != NULL) {
          COPY_4V(params, v);
       }
    }
@@ -573,13 +655,8 @@ _mesa_GetVertexAttribdvARB(GLuint index, GLenum pname, GLdouble *params)
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
-      if (index == 0) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetVertexAttribdv(index==0)");
-      }
-      else {
-         const GLfloat *v = ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + index];
-         FLUSH_CURRENT(ctx, 0);
+      const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribdv");
+      if (v != NULL) {
          params[0] = (GLdouble) v[0];
          params[1] = (GLdouble) v[1];
          params[2] = (GLdouble) v[2];
@@ -600,13 +677,8 @@ _mesa_GetVertexAttribivARB(GLuint index, GLenum pname, GLint *params)
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
-      if (index == 0) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetVertexAttribiv(index==0)");
-      }
-      else {
-         const GLfloat *v = ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + index];
-         FLUSH_CURRENT(ctx, 0);
+      const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribiv");
+      if (v != NULL) {
          /* XXX should floats in[0,1] be scaled to full int range? */
          params[0] = (GLint) v[0];
          params[1] = (GLint) v[1];
@@ -629,13 +701,9 @@ _mesa_GetVertexAttribIiv(GLuint index, GLenum pname, GLint *params)
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
-      if (index == 0) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetVertexAttribIiv(index==0)");
-      }
-      else {
-         const GLfloat *v = ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + index];
-         FLUSH_CURRENT(ctx, 0);
+      const GLfloat *v =
+        get_current_attrib(ctx, index, "glGetVertexAttribIiv");
+      if (v != NULL) {
          /* XXX we don't have true integer-valued vertex attribs yet */
          params[0] = (GLint) v[0];
          params[1] = (GLint) v[1];
@@ -658,13 +726,9 @@ _mesa_GetVertexAttribIuiv(GLuint index, GLenum pname, GLuint *params)
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
-      if (index == 0) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glGetVertexAttribIuiv(index==0)");
-      }
-      else {
-         const GLfloat *v = ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + index];
-         FLUSH_CURRENT(ctx, 0);
+      const GLfloat *v =
+        get_current_attrib(ctx, index, "glGetVertexAttribIuiv");
+      if (v != NULL) {
          /* XXX we don't have true integer-valued vertex attribs yet */
          params[0] = (GLuint) v[0];
          params[1] = (GLuint) v[1];
@@ -695,9 +759,9 @@ _mesa_GetVertexAttribPointervARB(GLuint index, GLenum pname, GLvoid **pointer)
       return;
    }
 
-   ASSERT(index < Elements(ctx->Array.ArrayObj->VertexAttrib));
+   ASSERT(VERT_ATTRIB_GENERIC(index) < Elements(ctx->Array.ArrayObj->VertexAttrib));
 
-   *pointer = (GLvoid *) ctx->Array.ArrayObj->VertexAttrib[index].Ptr;
+   *pointer = (GLvoid *) ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Ptr;
 }
 
 
@@ -958,7 +1022,6 @@ _mesa_LockArraysEXT(GLint first, GLsizei count)
    ctx->Array.LockCount = count;
 
    ctx->NewState |= _NEW_ARRAY;
-   ctx->Array.NewState |= _NEW_ARRAY_ALL;
 }
 
 
@@ -979,7 +1042,6 @@ _mesa_UnlockArraysEXT( void )
    ctx->Array.LockFirst = 0;
    ctx->Array.LockCount = 0;
    ctx->NewState |= _NEW_ARRAY;
-   ctx->Array.NewState |= _NEW_ARRAY_ALL;
 }
 
 
@@ -1051,20 +1113,55 @@ _mesa_PrimitiveRestartIndex(GLuint index)
 {
    GET_CURRENT_CONTEXT(ctx);
 
-   if (!ctx->Extensions.NV_primitive_restart &&
-       ctx->VersionMajor * 10 + ctx->VersionMinor < 31) {
+   if (!ctx->Extensions.NV_primitive_restart && ctx->Version < 31) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glPrimitiveRestartIndexNV()");
       return;
    }
 
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+   if (ctx->Array.RestartIndex != index) {
+      FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
+      ctx->Array.RestartIndex = index;
+   }
+}
+
+
+/**
+ * See GL_ARB_instanced_arrays.
+ * Note that the instance divisor only applies to generic arrays, not
+ * the legacy vertex arrays.
+ */
+void GLAPIENTRY
+_mesa_VertexAttribDivisor(GLuint index, GLuint divisor)
+{
+   struct gl_client_array *array;
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+   if (!ctx->Extensions.ARB_instanced_arrays) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glVertexAttribDivisor()");
+      return;
+   }
 
-   FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
+   if (index >= ctx->Const.VertexProgram.MaxAttribs) {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glVertexAttribDivisor(index = %u)",
+                  index);
+      return;
+   }
 
-   ctx->Array.RestartIndex = index;
+   ASSERT(VERT_ATTRIB_GENERIC(index) < Elements(ctx->Array.ArrayObj->VertexAttrib));
+
+   array = &ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(index)];
+   if (array->InstanceDivisor != divisor) {
+      FLUSH_VERTICES(ctx, _NEW_ARRAY);
+      array->InstanceDivisor = divisor;
+      ctx->Array.ArrayObj->NewArrays |= VERT_BIT(VERT_ATTRIB_GENERIC(index));
+   }
 }
 
 
+
 /**
  * Copy one client vertex array to another.
  */
@@ -1082,6 +1179,7 @@ _mesa_copy_client_array(struct gl_context *ctx,
    dst->Enabled = src->Enabled;
    dst->Normalized = src->Normalized;
    dst->Integer = src->Integer;
+   dst->InstanceDivisor = src->InstanceDivisor;
    dst->_ElementSize = src->_ElementSize;
    _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
    dst->_MaxElement = src->_MaxElement;
@@ -1119,18 +1217,18 @@ _mesa_print_arrays(struct gl_context *ctx)
    _mesa_update_array_object_max_element(ctx, arrayObj);
 
    printf("Array Object %u\n", arrayObj->Name);
-   if (arrayObj->Vertex.Enabled)
-      print_array("Vertex", -1, &arrayObj->Vertex);
-   if (arrayObj->Normal.Enabled)
-      print_array("Normal", -1, &arrayObj->Normal);
-   if (arrayObj->Color.Enabled)
-      print_array("Color", -1, &arrayObj->Color);
-   for (i = 0; i < Elements(arrayObj->TexCoord); i++)
-      if (arrayObj->TexCoord[i].Enabled)
-         print_array("TexCoord", i, &arrayObj->TexCoord[i]);
-   for (i = 0; i < Elements(arrayObj->VertexAttrib); i++)
-      if (arrayObj->VertexAttrib[i].Enabled)
-         print_array("Attrib", i, &arrayObj->VertexAttrib[i]);
+   if (arrayObj->VertexAttrib[VERT_ATTRIB_POS].Enabled)
+      print_array("Vertex", -1, &arrayObj->VertexAttrib[VERT_ATTRIB_POS]);
+   if (arrayObj->VertexAttrib[VERT_ATTRIB_NORMAL].Enabled)
+      print_array("Normal", -1, &arrayObj->VertexAttrib[VERT_ATTRIB_NORMAL]);
+   if (arrayObj->VertexAttrib[VERT_ATTRIB_COLOR0].Enabled)
+      print_array("Color", -1, &arrayObj->VertexAttrib[VERT_ATTRIB_COLOR0]);
+   for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++)
+      if (arrayObj->VertexAttrib[VERT_ATTRIB_TEX(i)].Enabled)
+         print_array("TexCoord", i, &arrayObj->VertexAttrib[VERT_ATTRIB_TEX(i)]);
+   for (i = 0; i < VERT_ATTRIB_GENERIC_MAX; i++)
+      if (arrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(i)].Enabled)
+         print_array("Attrib", i, &arrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(i)]);
    printf("  _MaxElement = %u\n", arrayObj->_MaxElement);
 }
 
@@ -1141,7 +1239,7 @@ _mesa_print_arrays(struct gl_context *ctx)
 void 
 _mesa_init_varray(struct gl_context *ctx)
 {
-   ctx->Array.DefaultArrayObj = _mesa_new_array_object(ctx, 0);
+   ctx->Array.DefaultArrayObj = ctx->Driver.NewArrayObject(ctx, 0);
    _mesa_reference_array_object(ctx, &ctx->Array.ArrayObj,
                                 ctx->Array.DefaultArrayObj);
    ctx->Array.ActiveTexture = 0;   /* GL_ARB_multitexture */