mesa: update fallthrough comment so gcc can see it
[mesa.git] / src / mesa / main / attrib.c
index b3d10d31edfa2cb430bb4eafa2672791b176b0e5..45018188168690760762db628fdfa48699fa0c53 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.6
  *
  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  * Copyright (C) 2009  VMware, Inc.   All Rights Reserved.
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
 #include "glheader.h"
-#include "imports.h"
+
 #include "accum.h"
 #include "arrayobj.h"
 #include "attrib.h"
@@ -32,7 +32,6 @@
 #include "buffers.h"
 #include "bufferobj.h"
 #include "clear.h"
-#include "colormac.h"
 #include "context.h"
 #include "depth.h"
 #include "enable.h"
@@ -43,8 +42,8 @@
 #include "lines.h"
 #include "macros.h"
 #include "matrix.h"
-#include "mfeatures.h"
 #include "multisample.h"
+#include "pixelstore.h"
 #include "points.h"
 #include "polygon.h"
 #include "shared.h"
 #include "varray.h"
 #include "viewport.h"
 #include "mtypes.h"
-#include "main/dispatch.h"
+#include "state.h"
 #include "hash.h"
 #include <stdbool.h>
+#include "util/u_memory.h"
 
 
 /**
@@ -74,7 +74,8 @@ struct gl_enable_attrib
    GLbitfield ClipPlanes;
    GLboolean ColorMaterial;
    GLboolean CullFace;
-   GLboolean DepthClamp;
+   GLboolean DepthClampNear;
+   GLboolean DepthClampFar;
    GLboolean DepthTest;
    GLboolean Dither;
    GLboolean Fog;
@@ -94,7 +95,6 @@ struct gl_enable_attrib
    GLboolean Map1TextureCoord4;
    GLboolean Map1Vertex3;
    GLboolean Map1Vertex4;
-   GLboolean Map1Attrib[16];  /* GL_NV_vertex_program */
    GLboolean Map2Color4;
    GLboolean Map2Index;
    GLboolean Map2Normal;
@@ -104,7 +104,6 @@ struct gl_enable_attrib
    GLboolean Map2TextureCoord4;
    GLboolean Map2Vertex3;
    GLboolean Map2Vertex4;
-   GLboolean Map2Attrib[16];  /* GL_NV_vertex_program */
 
    GLboolean Normalize;
    GLboolean PixelTexture;
@@ -115,7 +114,7 @@ struct gl_enable_attrib
    GLboolean PolygonSmooth;
    GLboolean PolygonStipple;
    GLboolean RescaleNormals;
-   GLboolean Scissor;
+   GLbitfield Scissor;
    GLboolean Stencil;
    GLboolean StencilTwoSide;          /* GL_EXT_stencil_two_side */
    GLboolean MultisampleEnabled;      /* GL_ARB_multisample */
@@ -127,17 +126,23 @@ struct gl_enable_attrib
    GLbitfield Texture[MAX_TEXTURE_UNITS];
    GLbitfield TexGen[MAX_TEXTURE_UNITS];
 
-   /* GL_ARB_vertex_program / GL_NV_vertex_program */
+   /* GL_ARB_vertex_program */
    GLboolean VertexProgram;
    GLboolean VertexProgramPointSize;
    GLboolean VertexProgramTwoSide;
 
+   /* GL_ARB_fragment_program */
+   GLboolean FragmentProgram;
+
    /* GL_ARB_point_sprite / GL_NV_point_sprite */
    GLboolean PointSprite;
    GLboolean FragmentShaderATI;
 
    /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
    GLboolean sRGBEnabled;
+
+   /* GL_NV_conservative_raster */
+   GLboolean ConservativeRasterization;
 };
 
 
@@ -178,14 +183,22 @@ struct texture_state
 };
 
 
-#if FEATURE_attrib_stack
+struct viewport_state
+{
+   struct gl_viewport_attrib ViewportArray[MAX_VIEWPORTS];
+   GLuint SubpixelPrecisionBias[2];
+};
+
+
+/** An unused GL_*_BIT value */
+#define DUMMY_BIT 0x10000000
 
 
 /**
  * Allocate new attribute node of given type/kind.  Attach payload data.
  * Insert it into the linked list named by 'head'.
  */
-static void
+static bool
 save_attrib_data(struct gl_attrib_node **head,
                  GLbitfield kind, void *payload)
 {
@@ -199,7 +212,42 @@ save_attrib_data(struct gl_attrib_node **head,
    }
    else {
       /* out of memory! */
+      return false;
+   }
+   return true;
+}
+
+
+/**
+ * Helper function for_mesa_PushAttrib for simple attributes.
+ * Allocates memory for attribute data and copies the given attribute data.
+ * \param head  head of linked list to insert attribute data into
+ * \param attr_bit  one of the GL_<attrib>_BIT flags
+ * \param attr_size  number of bytes to allocate for attribute data
+ * \param attr_data  the attribute data to copy
+ * \return true for success, false for out of memory
+ */
+static bool
+push_attrib(struct gl_context *ctx, struct gl_attrib_node **head,
+            GLbitfield attr_bit, GLuint attr_size, const void *attr_data)
+{
+   void *attribute;
+
+   attribute = malloc(attr_size);
+   if (attribute == NULL) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
+      return false;
+   }
+
+   if (save_attrib_data(head, attr_bit, attribute)) {
+      memcpy(attribute, attr_data, attr_size);
    }
+   else {
+      free(attribute);
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
+      return false;
+   }
+   return true;
 }
 
 
@@ -209,13 +257,12 @@ _mesa_PushAttrib(GLbitfield mask)
    struct gl_attrib_node *head;
 
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "glPushAttrib %x\n", (int) mask);
 
    if (ctx->AttribStackDepth >= MAX_ATTRIB_STACK_DEPTH) {
-      _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushAttrib" );
+      _mesa_error(ctx, GL_STACK_OVERFLOW, "glPushAttrib");
       return;
    }
 
@@ -223,43 +270,68 @@ _mesa_PushAttrib(GLbitfield mask)
    /* groups specified by the mask. */
    head = NULL;
 
+   if (mask == 0) {
+      /* if mask is zero we still need to push something so that we
+       * don't get a GL_STACK_UNDERFLOW error in glPopAttrib().
+       */
+      GLuint dummy = 0;
+      if (!push_attrib(ctx, &head, DUMMY_BIT, sizeof(dummy), &dummy))
+         goto end;
+   }
+
    if (mask & GL_ACCUM_BUFFER_BIT) {
-      struct gl_accum_attrib *attr;
-      attr = MALLOC_STRUCT( gl_accum_attrib );
-      memcpy( attr, &ctx->Accum, sizeof(struct gl_accum_attrib) );
-      save_attrib_data(&head, GL_ACCUM_BUFFER_BIT, attr);
+      if (!push_attrib(ctx, &head, GL_ACCUM_BUFFER_BIT,
+                       sizeof(struct gl_accum_attrib),
+                       (void*)&ctx->Accum))
+         goto end;
    }
 
    if (mask & GL_COLOR_BUFFER_BIT) {
       GLuint i;
       struct gl_colorbuffer_attrib *attr;
-      attr = MALLOC_STRUCT( gl_colorbuffer_attrib );
-      memcpy( attr, &ctx->Color, sizeof(struct gl_colorbuffer_attrib) );
-      /* push the Draw FBO's DrawBuffer[] state, not ctx->Color.DrawBuffer[] */
-      for (i = 0; i < ctx->Const.MaxDrawBuffers; i ++)
-         attr->DrawBuffer[i] = ctx->DrawBuffer->ColorDrawBuffer[i];
-      save_attrib_data(&head, GL_COLOR_BUFFER_BIT, attr);
+      attr = MALLOC_STRUCT(gl_colorbuffer_attrib);
+      if (attr == NULL) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
+         goto end;
+      }
+
+    if (save_attrib_data(&head, GL_COLOR_BUFFER_BIT, attr)) {
+         memcpy(attr, &ctx->Color, sizeof(struct gl_colorbuffer_attrib));
+         /* push the Draw FBO's DrawBuffer[] state, not ctx->Color.DrawBuffer[] */
+         for (i = 0; i < ctx->Const.MaxDrawBuffers; i ++)
+            attr->DrawBuffer[i] = ctx->DrawBuffer->ColorDrawBuffer[i];
+      }
+      else {
+         free(attr);
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
+         goto end;
+      }
    }
 
    if (mask & GL_CURRENT_BIT) {
-      struct gl_current_attrib *attr;
-      FLUSH_CURRENT( ctx, 0 );
-      attr = MALLOC_STRUCT( gl_current_attrib );
-      memcpy( attr, &ctx->Current, sizeof(struct gl_current_attrib) );
-      save_attrib_data(&head, GL_CURRENT_BIT, attr);
+      FLUSH_CURRENT(ctx, 0);
+      if (!push_attrib(ctx, &head, GL_CURRENT_BIT,
+                       sizeof(struct gl_current_attrib),
+                       (void*)&ctx->Current))
+         goto end;
    }
 
    if (mask & GL_DEPTH_BUFFER_BIT) {
-      struct gl_depthbuffer_attrib *attr;
-      attr = MALLOC_STRUCT( gl_depthbuffer_attrib );
-      memcpy( attr, &ctx->Depth, sizeof(struct gl_depthbuffer_attrib) );
-      save_attrib_data(&head, GL_DEPTH_BUFFER_BIT, attr);
+      if (!push_attrib(ctx, &head, GL_DEPTH_BUFFER_BIT,
+                       sizeof(struct gl_depthbuffer_attrib),
+                       (void*)&ctx->Depth))
+         goto end;
    }
 
    if (mask & GL_ENABLE_BIT) {
       struct gl_enable_attrib *attr;
       GLuint i;
-      attr = MALLOC_STRUCT( gl_enable_attrib );
+      attr = MALLOC_STRUCT(gl_enable_attrib);
+      if (attr == NULL) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
+         goto end;
+      }
+
       /* Copy enable flags from all other attributes into the enable struct. */
       attr->AlphaTest = ctx->Color.AlphaEnabled;
       attr->AutoNormal = ctx->Eval.AutoNormal;
@@ -267,7 +339,8 @@ _mesa_PushAttrib(GLbitfield mask)
       attr->ClipPlanes = ctx->Transform.ClipPlanesEnabled;
       attr->ColorMaterial = ctx->Light.ColorMaterialEnabled;
       attr->CullFace = ctx->Polygon.CullFlag;
-      attr->DepthClamp = ctx->Transform.DepthClamp;
+      attr->DepthClampNear = ctx->Transform.DepthClampNear;
+      attr->DepthClampFar = ctx->Transform.DepthClampFar;
       attr->DepthTest = ctx->Depth.Test;
       attr->Dither = ctx->Color.DitherFlag;
       attr->Fog = ctx->Fog.Enabled;
@@ -288,7 +361,6 @@ _mesa_PushAttrib(GLbitfield mask)
       attr->Map1TextureCoord4 = ctx->Eval.Map1TextureCoord4;
       attr->Map1Vertex3 = ctx->Eval.Map1Vertex3;
       attr->Map1Vertex4 = ctx->Eval.Map1Vertex4;
-      memcpy(attr->Map1Attrib, ctx->Eval.Map1Attrib, sizeof(ctx->Eval.Map1Attrib));
       attr->Map2Color4 = ctx->Eval.Map2Color4;
       attr->Map2Index = ctx->Eval.Map2Index;
       attr->Map2Normal = ctx->Eval.Map2Normal;
@@ -298,7 +370,6 @@ _mesa_PushAttrib(GLbitfield mask)
       attr->Map2TextureCoord4 = ctx->Eval.Map2TextureCoord4;
       attr->Map2Vertex3 = ctx->Eval.Map2Vertex3;
       attr->Map2Vertex4 = ctx->Eval.Map2Vertex4;
-      memcpy(attr->Map2Attrib, ctx->Eval.Map2Attrib, sizeof(ctx->Eval.Map2Attrib));
       attr->Normalize = ctx->Transform.Normalize;
       attr->RasterPositionUnclipped = ctx->Transform.RasterPositionUnclipped;
       attr->PointSmooth = ctx->Point.SmoothFlag;
@@ -309,7 +380,7 @@ _mesa_PushAttrib(GLbitfield mask)
       attr->PolygonSmooth = ctx->Polygon.SmoothFlag;
       attr->PolygonStipple = ctx->Polygon.StippleFlag;
       attr->RescaleNormals = ctx->Transform.RescaleNormals;
-      attr->Scissor = ctx->Scissor.Enabled;
+      attr->Scissor = ctx->Scissor.EnableFlags;
       attr->Stencil = ctx->Stencil.Enabled;
       attr->StencilTwoSide = ctx->Stencil.TestTwoSide;
       attr->MultisampleEnabled = ctx->Multisample.Enabled;
@@ -317,104 +388,126 @@ _mesa_PushAttrib(GLbitfield mask)
       attr->SampleAlphaToOne = ctx->Multisample.SampleAlphaToOne;
       attr->SampleCoverage = ctx->Multisample.SampleCoverage;
       for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
-         attr->Texture[i] = ctx->Texture.Unit[i].Enabled;
-         attr->TexGen[i] = ctx->Texture.Unit[i].TexGenEnabled;
+         attr->Texture[i] = ctx->Texture.FixedFuncUnit[i].Enabled;
+         attr->TexGen[i] = ctx->Texture.FixedFuncUnit[i].TexGenEnabled;
       }
-      /* GL_NV_vertex_program */
+      /* GL_ARB_vertex_program */
       attr->VertexProgram = ctx->VertexProgram.Enabled;
       attr->VertexProgramPointSize = ctx->VertexProgram.PointSizeEnabled;
       attr->VertexProgramTwoSide = ctx->VertexProgram.TwoSideEnabled;
-      save_attrib_data(&head, GL_ENABLE_BIT, attr);
+
+      /* GL_ARB_fragment_program */
+      attr->FragmentProgram = ctx->FragmentProgram.Enabled;
+
+      if (!save_attrib_data(&head, GL_ENABLE_BIT, attr)) {
+         free(attr);
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
+         goto end;
+      }
 
       /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
       attr->sRGBEnabled = ctx->Color.sRGBEnabled;
+
+      /* GL_NV_conservative_raster */
+      attr->ConservativeRasterization = ctx->ConservativeRasterization;
    }
 
    if (mask & GL_EVAL_BIT) {
-      struct gl_eval_attrib *attr;
-      attr = MALLOC_STRUCT( gl_eval_attrib );
-      memcpy( attr, &ctx->Eval, sizeof(struct gl_eval_attrib) );
-      save_attrib_data(&head, GL_EVAL_BIT, attr);
+      if (!push_attrib(ctx, &head, GL_EVAL_BIT,
+                       sizeof(struct gl_eval_attrib),
+                       (void*)&ctx->Eval))
+         goto end;
    }
 
    if (mask & GL_FOG_BIT) {
-      struct gl_fog_attrib *attr;
-      attr = MALLOC_STRUCT( gl_fog_attrib );
-      memcpy( attr, &ctx->Fog, sizeof(struct gl_fog_attrib) );
-      save_attrib_data(&head, GL_FOG_BIT, attr);
+      if (!push_attrib(ctx, &head, GL_FOG_BIT,
+                       sizeof(struct gl_fog_attrib),
+                       (void*)&ctx->Fog))
+         goto end;
    }
 
    if (mask & GL_HINT_BIT) {
-      struct gl_hint_attrib *attr;
-      attr = MALLOC_STRUCT( gl_hint_attrib );
-      memcpy( attr, &ctx->Hint, sizeof(struct gl_hint_attrib) );
-      save_attrib_data(&head, GL_HINT_BIT, attr);
+      if (!push_attrib(ctx, &head, GL_HINT_BIT,
+                       sizeof(struct gl_hint_attrib),
+                       (void*)&ctx->Hint))
+         goto end;
    }
 
    if (mask & GL_LIGHTING_BIT) {
-      struct gl_light_attrib *attr;
-      FLUSH_CURRENT(ctx, 0);   /* flush material changes */
-      attr = MALLOC_STRUCT( gl_light_attrib );
-      memcpy( attr, &ctx->Light, sizeof(struct gl_light_attrib) );
-      save_attrib_data(&head, GL_LIGHTING_BIT, attr);
+      FLUSH_CURRENT(ctx, 0);   /* flush material changes */
+      if (!push_attrib(ctx, &head, GL_LIGHTING_BIT,
+                       sizeof(struct gl_light_attrib),
+                       (void*)&ctx->Light))
+         goto end;
    }
 
    if (mask & GL_LINE_BIT) {
-      struct gl_line_attrib *attr;
-      attr = MALLOC_STRUCT( gl_line_attrib );
-      memcpy( attr, &ctx->Line, sizeof(struct gl_line_attrib) );
-      save_attrib_data(&head, GL_LINE_BIT, attr);
+      if (!push_attrib(ctx, &head, GL_LINE_BIT,
+                       sizeof(struct gl_line_attrib),
+                       (void*)&ctx->Line))
+         goto end;
    }
 
    if (mask & GL_LIST_BIT) {
-      struct gl_list_attrib *attr;
-      attr = MALLOC_STRUCT( gl_list_attrib );
-      memcpy( attr, &ctx->List, sizeof(struct gl_list_attrib) );
-      save_attrib_data(&head, GL_LIST_BIT, attr);
+      if (!push_attrib(ctx, &head, GL_LIST_BIT,
+                       sizeof(struct gl_list_attrib),
+                       (void*)&ctx->List))
+         goto end;
    }
 
    if (mask & GL_PIXEL_MODE_BIT) {
       struct gl_pixel_attrib *attr;
-      attr = MALLOC_STRUCT( gl_pixel_attrib );
-      memcpy( attr, &ctx->Pixel, sizeof(struct gl_pixel_attrib) );
-      /* push the Read FBO's ReadBuffer state, not ctx->Pixel.ReadBuffer */
-      attr->ReadBuffer = ctx->ReadBuffer->ColorReadBuffer;
-      save_attrib_data(&head, GL_PIXEL_MODE_BIT, attr);
+      attr = MALLOC_STRUCT(gl_pixel_attrib);
+      if (attr == NULL) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
+         goto end;
+      }
+
+      if (save_attrib_data(&head, GL_PIXEL_MODE_BIT, attr)) {
+         memcpy(attr, &ctx->Pixel, sizeof(struct gl_pixel_attrib));
+         /* push the Read FBO's ReadBuffer state, not ctx->Pixel.ReadBuffer */
+         attr->ReadBuffer = ctx->ReadBuffer->ColorReadBuffer;
+      }
+      else {
+         free(attr);
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
+         goto end;
+      }
    }
 
    if (mask & GL_POINT_BIT) {
-      struct gl_point_attrib *attr;
-      attr = MALLOC_STRUCT( gl_point_attrib );
-      memcpy( attr, &ctx->Point, sizeof(struct gl_point_attrib) );
-      save_attrib_data(&head, GL_POINT_BIT, attr);
+      if (!push_attrib(ctx, &head, GL_POINT_BIT,
+                       sizeof(struct gl_point_attrib),
+                       (void*)&ctx->Point))
+         goto end;
    }
 
    if (mask & GL_POLYGON_BIT) {
-      struct gl_polygon_attrib *attr;
-      attr = MALLOC_STRUCT( gl_polygon_attrib );
-      memcpy( attr, &ctx->Polygon, sizeof(struct gl_polygon_attrib) );
-      save_attrib_data(&head, GL_POLYGON_BIT, attr);
+      if (!push_attrib(ctx, &head, GL_POLYGON_BIT,
+                       sizeof(struct gl_polygon_attrib),
+                       (void*)&ctx->Polygon))
+         goto end;
    }
 
    if (mask & GL_POLYGON_STIPPLE_BIT) {
-      GLuint *stipple;
-      stipple = (GLuint *) malloc( 32*sizeof(GLuint) );
-      memcpy( stipple, ctx->PolygonStipple, 32*sizeof(GLuint) );
-      save_attrib_data(&head, GL_POLYGON_STIPPLE_BIT, stipple);
+      if (!push_attrib(ctx, &head, GL_POLYGON_STIPPLE_BIT,
+                       sizeof(ctx->PolygonStipple),
+                       (void*)&ctx->PolygonStipple))
+         goto end;
    }
 
    if (mask & GL_SCISSOR_BIT) {
-      struct gl_scissor_attrib *attr;
-      attr = MALLOC_STRUCT( gl_scissor_attrib );
-      memcpy( attr, &ctx->Scissor, sizeof(struct gl_scissor_attrib) );
-      save_attrib_data(&head, GL_SCISSOR_BIT, attr);
+      if (!push_attrib(ctx, &head, GL_SCISSOR_BIT,
+                       sizeof(struct gl_scissor_attrib),
+                       (void*)&ctx->Scissor))
+         goto end;
    }
 
    if (mask & GL_STENCIL_BUFFER_BIT) {
-      struct gl_stencil_attrib *attr;
-      attr = MALLOC_STRUCT( gl_stencil_attrib );
-      memcpy( attr, &ctx->Stencil, sizeof(struct gl_stencil_attrib) );
-      save_attrib_data(&head, GL_STENCIL_BUFFER_BIT, attr);
+      if (!push_attrib(ctx, &head, GL_STENCIL_BUFFER_BIT,
+                       sizeof(struct gl_stencil_attrib),
+                       (void*)&ctx->Stencil))
+         goto end;
    }
 
    if (mask & GL_TEXTURE_BIT) {
@@ -426,6 +519,12 @@ _mesa_PushAttrib(GLbitfield mask)
          goto end;
       }
 
+      if (!save_attrib_data(&head, GL_TEXTURE_BIT, texstate)) {
+         free(texstate);
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib(GL_TEXTURE_BIT)");
+         goto end;
+      }
+
       _mesa_lock_context_textures(ctx);
 
       /* copy/save the bulk of texture state here */
@@ -452,35 +551,48 @@ _mesa_PushAttrib(GLbitfield mask)
       _mesa_reference_shared_state(ctx, &texstate->SharedRef, ctx->Shared);
 
       _mesa_unlock_context_textures(ctx);
-
-      save_attrib_data(&head, GL_TEXTURE_BIT, texstate);
    }
 
    if (mask & GL_TRANSFORM_BIT) {
-      struct gl_transform_attrib *attr;
-      attr = MALLOC_STRUCT( gl_transform_attrib );
-      memcpy( attr, &ctx->Transform, sizeof(struct gl_transform_attrib) );
-      save_attrib_data(&head, GL_TRANSFORM_BIT, attr);
+      if (!push_attrib(ctx, &head, GL_TRANSFORM_BIT,
+                       sizeof(struct gl_transform_attrib),
+                       (void*)&ctx->Transform))
+         goto end;
    }
 
    if (mask & GL_VIEWPORT_BIT) {
-      struct gl_viewport_attrib *attr;
-      attr = MALLOC_STRUCT( gl_viewport_attrib );
-      memcpy( attr, &ctx->Viewport, sizeof(struct gl_viewport_attrib) );
-      save_attrib_data(&head, GL_VIEWPORT_BIT, attr);
+      struct viewport_state *viewstate = CALLOC_STRUCT(viewport_state);
+      if (!viewstate) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib(GL_VIEWPORT_BIT)");
+         goto end;
+      }
+
+      if (!save_attrib_data(&head, GL_VIEWPORT_BIT, viewstate)) {
+         free(viewstate);
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib(GL_VIEWPORT_BIT)");
+         goto end;
+      }
+
+      memcpy(&viewstate->ViewportArray, &ctx->ViewportArray,
+             sizeof(struct gl_viewport_attrib)*ctx->Const.MaxViewports);
+
+      viewstate->SubpixelPrecisionBias[0] = ctx->SubpixelPrecisionBias[0];
+      viewstate->SubpixelPrecisionBias[1] = ctx->SubpixelPrecisionBias[1];
    }
 
    /* GL_ARB_multisample */
    if (mask & GL_MULTISAMPLE_BIT_ARB) {
-      struct gl_multisample_attrib *attr;
-      attr = MALLOC_STRUCT( gl_multisample_attrib );
-      memcpy( attr, &ctx->Multisample, sizeof(struct gl_multisample_attrib) );
-      save_attrib_data(&head, GL_MULTISAMPLE_BIT_ARB, attr);
+      if (!push_attrib(ctx, &head, GL_MULTISAMPLE_BIT_ARB,
+                       sizeof(struct gl_multisample_attrib),
+                       (void*)&ctx->Multisample))
+         goto end;
    }
 
 end:
-   ctx->AttribStack[ctx->AttribStackDepth] = head;
-   ctx->AttribStackDepth++;
+   if (head != NULL) {
+       ctx->AttribStack[ctx->AttribStackDepth] = head;
+       ctx->AttribStackDepth++;
+   }
 }
 
 
@@ -491,10 +603,10 @@ pop_enable_group(struct gl_context *ctx, const struct gl_enable_attrib *enable)
    const GLuint curTexUnitSave = ctx->Texture.CurrentUnit;
    GLuint i;
 
-#define TEST_AND_UPDATE(VALUE, NEWVALUE, ENUM)         \
-       if ((VALUE) != (NEWVALUE)) {                    \
-          _mesa_set_enable( ctx, ENUM, (NEWVALUE) );   \
-       }
+#define TEST_AND_UPDATE(VALUE, NEWVALUE, ENUM)                \
+        if ((VALUE) != (NEWVALUE)) {                        \
+           _mesa_set_enable(ctx, ENUM, (NEWVALUE));        \
+        }
 
    TEST_AND_UPDATE(ctx->Color.AlphaEnabled, enable->AlphaTest, GL_ALPHA_TEST);
    if (ctx->Color.BlendEnabled != enable->Blend) {
@@ -512,15 +624,25 @@ pop_enable_group(struct gl_context *ctx, const struct gl_enable_attrib *enable)
    for (i=0;i<ctx->Const.MaxClipPlanes;i++) {
       const GLuint mask = 1 << i;
       if ((ctx->Transform.ClipPlanesEnabled & mask) != (enable->ClipPlanes & mask))
-         _mesa_set_enable(ctx, (GLenum) (GL_CLIP_PLANE0 + i),
-                          !!(enable->ClipPlanes & mask));
+          _mesa_set_enable(ctx, (GLenum) (GL_CLIP_PLANE0 + i),
+                           !!(enable->ClipPlanes & mask));
    }
 
    TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled, enable->ColorMaterial,
                    GL_COLOR_MATERIAL);
    TEST_AND_UPDATE(ctx->Polygon.CullFlag, enable->CullFace, GL_CULL_FACE);
-   TEST_AND_UPDATE(ctx->Transform.DepthClamp, enable->DepthClamp,
-                  GL_DEPTH_CLAMP);
+
+   if (!ctx->Extensions.AMD_depth_clamp_separate) {
+      TEST_AND_UPDATE(ctx->Transform.DepthClampNear && ctx->Transform.DepthClampFar,
+                      enable->DepthClampNear && enable->DepthClampFar,
+                      GL_DEPTH_CLAMP);
+   } else {
+      TEST_AND_UPDATE(ctx->Transform.DepthClampNear, enable->DepthClampNear,
+                      GL_DEPTH_CLAMP_NEAR_AMD);
+      TEST_AND_UPDATE(ctx->Transform.DepthClampFar, enable->DepthClampFar,
+                      GL_DEPTH_CLAMP_FAR_AMD);
+   }
+
    TEST_AND_UPDATE(ctx->Depth.Test, enable->DepthTest, GL_DEPTH_TEST);
    TEST_AND_UPDATE(ctx->Color.DitherFlag, enable->Dither, GL_DITHER);
    TEST_AND_UPDATE(ctx->Fog.Enabled, enable->Fog, GL_FOG);
@@ -548,10 +670,6 @@ pop_enable_group(struct gl_context *ctx, const struct gl_enable_attrib *enable)
                    GL_MAP1_VERTEX_3);
    TEST_AND_UPDATE(ctx->Eval.Map1Vertex4, enable->Map1Vertex4,
                    GL_MAP1_VERTEX_4);
-   for (i = 0; i < 16; i++) {
-      TEST_AND_UPDATE(ctx->Eval.Map1Attrib[i], enable->Map1Attrib[i],
-                      GL_MAP1_VERTEX_ATTRIB0_4_NV + i);
-   }
 
    TEST_AND_UPDATE(ctx->Eval.Map2Color4, enable->Map2Color4, GL_MAP2_COLOR_4);
    TEST_AND_UPDATE(ctx->Eval.Map2Index, enable->Map2Index, GL_MAP2_INDEX);
@@ -568,10 +686,6 @@ pop_enable_group(struct gl_context *ctx, const struct gl_enable_attrib *enable)
                    GL_MAP2_VERTEX_3);
    TEST_AND_UPDATE(ctx->Eval.Map2Vertex4, enable->Map2Vertex4,
                    GL_MAP2_VERTEX_4);
-   for (i = 0; i < 16; i++) {
-      TEST_AND_UPDATE(ctx->Eval.Map2Attrib[i], enable->Map2Attrib[i],
-                      GL_MAP2_VERTEX_ATTRIB0_4_NV + i);
-   }
 
    TEST_AND_UPDATE(ctx->Eval.AutoNormal, enable->AutoNormal, GL_AUTO_NORMAL);
    TEST_AND_UPDATE(ctx->Transform.Normalize, enable->Normalize, GL_NORMALIZE);
@@ -596,7 +710,13 @@ pop_enable_group(struct gl_context *ctx, const struct gl_enable_attrib *enable)
                    GL_POLYGON_SMOOTH);
    TEST_AND_UPDATE(ctx->Polygon.StippleFlag, enable->PolygonStipple,
                    GL_POLYGON_STIPPLE);
-   TEST_AND_UPDATE(ctx->Scissor.Enabled, enable->Scissor, GL_SCISSOR_TEST);
+   if (ctx->Scissor.EnableFlags != enable->Scissor) {
+      unsigned i;
+
+      for (i = 0; i < ctx->Const.MaxViewports; i++) {
+         _mesa_set_enablei(ctx, GL_SCISSOR_TEST, i, (enable->Scissor >> i) & 1);
+      }
+   }
    TEST_AND_UPDATE(ctx->Stencil.Enabled, enable->Stencil, GL_STENCIL_TEST);
    if (ctx->Extensions.EXT_stencil_two_side) {
       TEST_AND_UPDATE(ctx->Stencil.TestTwoSide, enable->StencilTwoSide, GL_STENCIL_TEST_TWO_SIDE_EXT);
@@ -612,7 +732,7 @@ pop_enable_group(struct gl_context *ctx, const struct gl_enable_attrib *enable)
    TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
                    enable->SampleCoverage,
                    GL_SAMPLE_COVERAGE_ARB);
-   /* GL_ARB_vertex_program, GL_NV_vertex_program */
+   /* GL_ARB_vertex_program */
    TEST_AND_UPDATE(ctx->VertexProgram.Enabled,
                    enable->VertexProgram,
                    GL_VERTEX_PROGRAM_ARB);
@@ -623,17 +743,29 @@ pop_enable_group(struct gl_context *ctx, const struct gl_enable_attrib *enable)
                    enable->VertexProgramTwoSide,
                    GL_VERTEX_PROGRAM_TWO_SIDE_ARB);
 
+   /* GL_ARB_fragment_program */
+   TEST_AND_UPDATE(ctx->FragmentProgram.Enabled,
+                   enable->FragmentProgram,
+                   GL_FRAGMENT_PROGRAM_ARB);
+
    /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
    TEST_AND_UPDATE(ctx->Color.sRGBEnabled, enable->sRGBEnabled,
                    GL_FRAMEBUFFER_SRGB);
 
+   /* GL_NV_conservative_raster */
+   if (ctx->Extensions.NV_conservative_raster) {
+      TEST_AND_UPDATE(ctx->ConservativeRasterization,
+                      enable->ConservativeRasterization,
+                      GL_CONSERVATIVE_RASTERIZATION_NV);
+   }
+
    /* texture unit enables */
    for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
       const GLbitfield enabled = enable->Texture[i];
       const GLbitfield genEnabled = enable->TexGen[i];
 
-      if (ctx->Texture.Unit[i].Enabled != enabled) {
-         _mesa_ActiveTextureARB(GL_TEXTURE0 + i);
+      if (ctx->Texture.FixedFuncUnit[i].Enabled != enabled) {
+         _mesa_ActiveTexture(GL_TEXTURE0 + i);
 
          _mesa_set_enable(ctx, GL_TEXTURE_1D, !!(enabled & TEXTURE_1D_BIT));
          _mesa_set_enable(ctx, GL_TEXTURE_2D, !!(enabled & TEXTURE_2D_BIT));
@@ -646,16 +778,10 @@ pop_enable_group(struct gl_context *ctx, const struct gl_enable_attrib *enable)
             _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP,
                              !!(enabled & TEXTURE_CUBE_BIT));
          }
-         if (ctx->Extensions.MESA_texture_array) {
-            _mesa_set_enable(ctx, GL_TEXTURE_1D_ARRAY_EXT,
-                             !!(enabled & TEXTURE_1D_ARRAY_BIT));
-            _mesa_set_enable(ctx, GL_TEXTURE_2D_ARRAY_EXT,
-                             !!(enabled & TEXTURE_2D_ARRAY_BIT));
-         }
       }
 
-      if (ctx->Texture.Unit[i].TexGenEnabled != genEnabled) {
-         _mesa_ActiveTextureARB(GL_TEXTURE0 + i);
+      if (ctx->Texture.FixedFuncUnit[i].TexGenEnabled != genEnabled) {
+         _mesa_ActiveTexture(GL_TEXTURE0 + i);
          _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, !!(genEnabled & S_BIT));
          _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, !!(genEnabled & T_BIT));
          _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, !!(genEnabled & R_BIT));
@@ -663,7 +789,7 @@ pop_enable_group(struct gl_context *ctx, const struct gl_enable_attrib *enable)
       }
    }
 
-   _mesa_ActiveTextureARB(GL_TEXTURE0 + curTexUnitSave);
+   _mesa_ActiveTexture(GL_TEXTURE0 + curTexUnitSave);
 }
 
 
@@ -678,28 +804,22 @@ pop_texture_group(struct gl_context *ctx, struct texture_state *texstate)
    _mesa_lock_context_textures(ctx);
 
    for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
-      const struct gl_texture_unit *unit = &texstate->Texture.Unit[u];
+      const struct gl_fixedfunc_texture_unit *unit =
+         &texstate->Texture.FixedFuncUnit[u];
       GLuint tgt;
 
-      _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + u);
+      _mesa_ActiveTexture(GL_TEXTURE0_ARB + u);
       _mesa_set_enable(ctx, GL_TEXTURE_1D, !!(unit->Enabled & TEXTURE_1D_BIT));
       _mesa_set_enable(ctx, GL_TEXTURE_2D, !!(unit->Enabled & TEXTURE_2D_BIT));
       _mesa_set_enable(ctx, GL_TEXTURE_3D, !!(unit->Enabled & TEXTURE_3D_BIT));
       if (ctx->Extensions.ARB_texture_cube_map) {
-         _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP_ARB,
+         _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP,
                           !!(unit->Enabled & TEXTURE_CUBE_BIT));
       }
       if (ctx->Extensions.NV_texture_rectangle) {
          _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE_NV,
                           !!(unit->Enabled & TEXTURE_RECT_BIT));
       }
-      if (ctx->Extensions.MESA_texture_array) {
-         _mesa_set_enable(ctx, GL_TEXTURE_1D_ARRAY_EXT,
-                          !!(unit->Enabled & TEXTURE_1D_ARRAY_BIT));
-         _mesa_set_enable(ctx, GL_TEXTURE_2D_ARRAY_EXT,
-                          !!(unit->Enabled & TEXTURE_2D_ARRAY_BIT));
-      }
-
       _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->EnvMode);
       _mesa_TexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, unit->EnvColor);
       _mesa_TexGeni(GL_S, GL_TEXTURE_GEN_MODE, unit->GenS.Mode);
@@ -712,7 +832,9 @@ pop_texture_group(struct gl_context *ctx, struct texture_state *texstate)
       _mesa_TexGenfv(GL_Q, GL_OBJECT_PLANE, unit->GenQ.ObjectPlane);
       /* Eye plane done differently to avoid re-transformation */
       {
-         struct gl_texture_unit *destUnit = &ctx->Texture.Unit[u];
+         struct gl_fixedfunc_texture_unit *destUnit =
+            &ctx->Texture.FixedFuncUnit[u];
+
          COPY_4FV(destUnit->GenS.EyePlane, unit->GenS.EyePlane);
          COPY_4FV(destUnit->GenT.EyePlane, unit->GenT.EyePlane);
          COPY_4FV(destUnit->GenR.EyePlane, unit->GenR.EyePlane);
@@ -729,11 +851,11 @@ pop_texture_group(struct gl_context *ctx, struct texture_state *texstate)
       _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, !!(unit->TexGenEnabled & R_BIT));
       _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, !!(unit->TexGenEnabled & Q_BIT));
       _mesa_TexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS,
-                   unit->LodBias);
+                   texstate->Texture.Unit[u].LodBias);
       _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,
-                   unit->Combine.ModeRGB);
+                    unit->Combine.ModeRGB);
       _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,
-                   unit->Combine.ModeA);
+                    unit->Combine.ModeA);
       {
          const GLuint n = ctx->Extensions.NV_texture_env_combine4 ? 4 : 3;
          GLuint i;
@@ -749,9 +871,9 @@ pop_texture_group(struct gl_context *ctx, struct texture_state *texstate)
          }
       }
       _mesa_TexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE,
-                   1 << unit->Combine.ScaleShiftRGB);
+                    1 << unit->Combine.ScaleShiftRGB);
       _mesa_TexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE,
-                   1 << unit->Combine.ScaleShiftA);
+                    1 << unit->Combine.ScaleShiftA);
 
       /* Restore texture object state for each target */
       for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
@@ -764,7 +886,7 @@ pop_texture_group(struct gl_context *ctx, struct texture_state *texstate)
          /* don't restore state for unsupported targets to prevent
           * raising GL errors.
           */
-         if (obj->Target == GL_TEXTURE_CUBE_MAP_ARB &&
+         if (obj->Target == GL_TEXTURE_CUBE_MAP &&
              !ctx->Extensions.ARB_texture_cube_map) {
             continue;
          }
@@ -774,13 +896,19 @@ pop_texture_group(struct gl_context *ctx, struct texture_state *texstate)
          }
          else if ((obj->Target == GL_TEXTURE_1D_ARRAY_EXT ||
                    obj->Target == GL_TEXTURE_2D_ARRAY_EXT) &&
-                  !ctx->Extensions.MESA_texture_array) {
+                  !ctx->Extensions.EXT_texture_array) {
             continue;
          }
-         else if (obj->Target == GL_TEXTURE_BUFFER)
+         else if (obj->Target == GL_TEXTURE_CUBE_MAP_ARRAY &&
+             !ctx->Extensions.ARB_texture_cube_map_array) {
+            continue;
+         } else if (obj->Target == GL_TEXTURE_BUFFER)
             continue;
          else if (obj->Target == GL_TEXTURE_EXTERNAL_OES)
             continue;
+         else if (obj->Target == GL_TEXTURE_2D_MULTISAMPLE ||
+                  obj->Target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
+            continue;
 
          target = obj->Target;
 
@@ -821,7 +949,7 @@ pop_texture_group(struct gl_context *ctx, struct texture_state *texstate)
       }
    }
 
-   _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + texstate->Texture.CurrentUnit);
+   _mesa_ActiveTexture(GL_TEXTURE0_ARB + texstate->Texture.CurrentUnit);
 
    _mesa_reference_shared_state(ctx, &texstate->SharedRef, NULL);
 
@@ -844,10 +972,10 @@ _mesa_PopAttrib(void)
 {
    struct gl_attrib_node *attr, *next;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+   FLUSH_VERTICES(ctx, 0);
 
    if (ctx->AttribStackDepth == 0) {
-      _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopAttrib" );
+      _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopAttrib");
       return;
    }
 
@@ -858,10 +986,14 @@ _mesa_PopAttrib(void)
 
       if (MESA_VERBOSE & VERBOSE_API) {
          _mesa_debug(ctx, "glPopAttrib %s\n",
-                     _mesa_lookup_enum_by_nr(attr->kind));
+                     _mesa_enum_to_string(attr->kind));
       }
 
       switch (attr->kind) {
+         case DUMMY_BIT:
+            /* do nothing */
+            break;
+
          case GL_ACCUM_BUFFER_BIT:
             {
                const struct gl_accum_attrib *accum;
@@ -884,19 +1016,19 @@ _mesa_PopAttrib(void)
                                 color->ClearColor.f[3]);
                _mesa_IndexMask(color->IndexMask);
                if (!ctx->Extensions.EXT_draw_buffers2) {
-                  _mesa_ColorMask((GLboolean) (color->ColorMask[0][0] != 0),
-                                  (GLboolean) (color->ColorMask[0][1] != 0),
-                                  (GLboolean) (color->ColorMask[0][2] != 0),
-                                  (GLboolean) (color->ColorMask[0][3] != 0));
+                  _mesa_ColorMask(GET_COLORMASK_BIT(color->ColorMask, 0, 0),
+                                  GET_COLORMASK_BIT(color->ColorMask, 0, 1),
+                                  GET_COLORMASK_BIT(color->ColorMask, 0, 2),
+                                  GET_COLORMASK_BIT(color->ColorMask, 0, 3));
                }
                else {
                   GLuint i;
                   for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
-                     _mesa_ColorMaskIndexed(i, 
-                                  (GLboolean) (color->ColorMask[i][0] != 0),
-                                  (GLboolean) (color->ColorMask[i][1] != 0),
-                                  (GLboolean) (color->ColorMask[i][2] != 0),
-                                  (GLboolean) (color->ColorMask[i][3] != 0));
+                     _mesa_ColorMaski(i,
+                                      GET_COLORMASK_BIT(color->ColorMask, i, 0),
+                                      GET_COLORMASK_BIT(color->ColorMask, i, 1),
+                                      GET_COLORMASK_BIT(color->ColorMask, i, 2),
+                                      GET_COLORMASK_BIT(color->ColorMask, i, 3));
                   }
                }
                {
@@ -907,13 +1039,13 @@ _mesa_PopAttrib(void)
                    * function, but legal for the later.
                    */
                   GLboolean multipleBuffers = GL_FALSE;
-                 GLuint i;
+                  GLuint i;
 
-                 for (i = 1; i < ctx->Const.MaxDrawBuffers; i++) {
-                    if (color->DrawBuffer[i] != GL_NONE) {
-                       multipleBuffers = GL_TRUE;
-                       break;
-                    }
+                  for (i = 1; i < ctx->Const.MaxDrawBuffers; i++) {
+                     if (color->DrawBuffer[i] != GL_NONE) {
+                        multipleBuffers = GL_TRUE;
+                        break;
+                     }
                   }
                   /* Call the API_level functions, not _mesa_drawbuffers()
                    * since we need to do error checking on the pop'd
@@ -922,11 +1054,16 @@ _mesa_PopAttrib(void)
                    * user FBO bound, GL_FRONT will be illegal and we'll need
                    * to record that error.  Per OpenGL ARB decision.
                    */
-                  if (multipleBuffers)
-                     _mesa_DrawBuffersARB(ctx->Const.MaxDrawBuffers,
-                                          color->DrawBuffer);
-                  else
+                  if (multipleBuffers) {
+                     GLenum buffers[MAX_DRAW_BUFFERS];
+
+                     for (unsigned i = 0; i < ctx->Const.MaxDrawBuffers; i++)
+                        buffers[i] = color->DrawBuffer[i];
+
+                     _mesa_DrawBuffers(ctx->Const.MaxDrawBuffers, buffers);
+                  } else {
                      _mesa_DrawBuffer(color->DrawBuffer[0]);
+                  }
                }
                _mesa_set_enable(ctx, GL_ALPHA_TEST, color->AlphaEnabled);
                _mesa_AlphaFunc(color->AlphaFunc, color->AlphaRefUnclamped);
@@ -947,18 +1084,18 @@ _mesa_PopAttrib(void)
                   /* set blend per buffer */
                   GLuint buf;
                   for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) {
-                     _mesa_BlendFuncSeparatei(buf, color->Blend[buf].SrcRGB,
+                     _mesa_BlendFuncSeparateiARB(buf, color->Blend[buf].SrcRGB,
                                               color->Blend[buf].DstRGB,
                                               color->Blend[buf].SrcA,
                                               color->Blend[buf].DstA);
-                     _mesa_BlendEquationSeparatei(buf,
+                     _mesa_BlendEquationSeparateiARB(buf,
                                                   color->Blend[buf].EquationRGB,
                                                   color->Blend[buf].EquationA);
                   }
                }
                else {
                   /* set same blend modes for all buffers */
-                  _mesa_BlendFuncSeparateEXT(color->Blend[0].SrcRGB,
+                  _mesa_BlendFuncSeparate(color->Blend[0].SrcRGB,
                                              color->Blend[0].DstRGB,
                                              color->Blend[0].SrcA,
                                              color->Blend[0].DstA);
@@ -970,7 +1107,7 @@ _mesa_PopAttrib(void)
                      _mesa_BlendEquation(color->Blend[0].EquationRGB);
                   }
                   else {
-                     _mesa_BlendEquationSeparateEXT(
+                     _mesa_BlendEquationSeparate(
                                                  color->Blend[0].EquationRGB,
                                                  color->Blend[0].EquationA);
                   }
@@ -985,8 +1122,11 @@ _mesa_PopAttrib(void)
                _mesa_set_enable(ctx, GL_INDEX_LOGIC_OP,
                                 color->IndexLogicOpEnabled);
                _mesa_set_enable(ctx, GL_DITHER, color->DitherFlag);
-               _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, color->ClampFragmentColor);
-               _mesa_ClampColorARB(GL_CLAMP_READ_COLOR_ARB, color->ClampReadColor);
+               if (ctx->Extensions.ARB_color_buffer_float)
+                  _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR_ARB,
+                                   color->ClampFragmentColor);
+               if (ctx->Extensions.ARB_color_buffer_float || ctx->Version >= 30)
+                  _mesa_ClampColor(GL_CLAMP_READ_COLOR_ARB, color->ClampReadColor);
 
                /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
                if (ctx->Extensions.EXT_framebuffer_sRGB)
@@ -994,9 +1134,9 @@ _mesa_PopAttrib(void)
             }
             break;
          case GL_CURRENT_BIT:
-           FLUSH_CURRENT( ctx, 0 );
-            memcpy( &ctx->Current, attr->data,
-                   sizeof(struct gl_current_attrib) );
+            FLUSH_CURRENT(ctx, 0);
+            memcpy(&ctx->Current, attr->data,
+                    sizeof(struct gl_current_attrib));
             break;
          case GL_DEPTH_BUFFER_BIT:
             {
@@ -1006,6 +1146,11 @@ _mesa_PopAttrib(void)
                _mesa_ClearDepth(depth->Clear);
                _mesa_set_enable(ctx, GL_DEPTH_TEST, depth->Test);
                _mesa_DepthMask(depth->Mask);
+               if (ctx->Extensions.EXT_depth_bounds_test) {
+                  _mesa_set_enable(ctx, GL_DEPTH_BOUNDS_TEST_EXT,
+                                   depth->BoundsTest);
+                  _mesa_DepthBoundsEXT(depth->BoundsMin, depth->BoundsMax);
+               }
             }
             break;
          case GL_ENABLE_BIT:
@@ -1013,12 +1158,27 @@ _mesa_PopAttrib(void)
                const struct gl_enable_attrib *enable;
                enable = (const struct gl_enable_attrib *) attr->data;
                pop_enable_group(ctx, enable);
-              ctx->NewState |= _NEW_ALL;
+               ctx->NewState |= _NEW_ALL;
+               ctx->NewDriverState |= ctx->DriverFlags.NewAlphaTest |
+                                      ctx->DriverFlags.NewBlend |
+                                      ctx->DriverFlags.NewClipPlaneEnable |
+                                      ctx->DriverFlags.NewDepth |
+                                      ctx->DriverFlags.NewDepthClamp |
+                                      ctx->DriverFlags.NewFramebufferSRGB |
+                                      ctx->DriverFlags.NewLineState |
+                                      ctx->DriverFlags.NewLogicOp |
+                                      ctx->DriverFlags.NewMultisampleEnable |
+                                      ctx->DriverFlags.NewPolygonState |
+                                      ctx->DriverFlags.NewSampleAlphaToXEnable |
+                                      ctx->DriverFlags.NewSampleMask |
+                                      ctx->DriverFlags.NewScissorTest |
+                                      ctx->DriverFlags.NewStencil |
+                                      ctx->DriverFlags.NewNvConservativeRasterization;
             }
             break;
          case GL_EVAL_BIT:
-            memcpy( &ctx->Eval, attr->data, sizeof(struct gl_eval_attrib) );
-           ctx->NewState |= _NEW_EVAL;
+            memcpy(&ctx->Eval, attr->data, sizeof(struct gl_eval_attrib));
+            vbo_exec_update_eval_maps(ctx);
             break;
          case GL_FOG_BIT:
             {
@@ -1038,15 +1198,13 @@ _mesa_PopAttrib(void)
                const struct gl_hint_attrib *hint;
                hint = (const struct gl_hint_attrib *) attr->data;
                _mesa_Hint(GL_PERSPECTIVE_CORRECTION_HINT,
-                          hint->PerspectiveCorrection );
+                          hint->PerspectiveCorrection);
                _mesa_Hint(GL_POINT_SMOOTH_HINT, hint->PointSmooth);
                _mesa_Hint(GL_LINE_SMOOTH_HINT, hint->LineSmooth);
                _mesa_Hint(GL_POLYGON_SMOOTH_HINT, hint->PolygonSmooth);
                _mesa_Hint(GL_FOG_HINT, hint->Fog);
-               _mesa_Hint(GL_CLIP_VOLUME_CLIPPING_HINT_EXT,
-                          hint->ClipVolumeClipping);
-              _mesa_Hint(GL_TEXTURE_COMPRESSION_HINT_ARB,
-                         hint->TextureCompression);
+               _mesa_Hint(GL_TEXTURE_COMPRESSION_HINT_ARB,
+                          hint->TextureCompression);
             }
             break;
          case GL_LIGHTING_BIT:
@@ -1058,14 +1216,14 @@ _mesa_PopAttrib(void)
                _mesa_set_enable(ctx, GL_LIGHTING, light->Enabled);
                /* per-light state */
                if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
-                  _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
-              
+                  _math_matrix_analyse(ctx->ModelviewMatrixStack.Top);
+
                for (i = 0; i < ctx->Const.MaxLights; i++) {
                   const struct gl_light *l = &light->Light[i];
                   _mesa_set_enable(ctx, GL_LIGHT0 + i, l->Enabled);
                   _mesa_light(ctx, i, GL_AMBIENT, l->Ambient);
                   _mesa_light(ctx, i, GL_DIFFUSE, l->Diffuse);
-                  _mesa_light(ctx, i, GL_SPECULAR, l->Specular );
+                  _mesa_light(ctx, i, GL_SPECULAR, l->Specular);
                   _mesa_light(ctx, i, GL_POSITION, l->EyePosition);
                   _mesa_light(ctx, i, GL_SPOT_DIRECTION, l->SpotDirection);
                   {
@@ -1113,7 +1271,10 @@ _mesa_PopAttrib(void)
                /* materials */
                memcpy(&ctx->Light.Material, &light->Material,
                       sizeof(struct gl_material));
-               _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, light->ClampVertexColor);
+               if (ctx->Extensions.ARB_color_buffer_float) {
+                  _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR_ARB,
+                                   light->ClampVertexColor);
+               }
             }
             break;
          case GL_LINE_BIT:
@@ -1127,13 +1288,13 @@ _mesa_PopAttrib(void)
             }
             break;
          case GL_LIST_BIT:
-            memcpy( &ctx->List, attr->data, sizeof(struct gl_list_attrib) );
+            memcpy(&ctx->List, attr->data, sizeof(struct gl_list_attrib));
             break;
          case GL_PIXEL_MODE_BIT:
-            memcpy( &ctx->Pixel, attr->data, sizeof(struct gl_pixel_attrib) );
+            memcpy(&ctx->Pixel, attr->data, sizeof(struct gl_pixel_attrib));
             /* XXX what other pixel state needs to be set by function calls? */
             _mesa_ReadBuffer(ctx->Pixel.ReadBuffer);
-           ctx->NewState |= _NEW_PIXEL;
+            ctx->NewState |= _NEW_PIXEL;
             break;
          case GL_POINT_BIT:
             {
@@ -1152,18 +1313,21 @@ _mesa_PopAttrib(void)
                                         point->Threshold);
                }
                if (ctx->Extensions.NV_point_sprite
-                  || ctx->Extensions.ARB_point_sprite) {
+                   || ctx->Extensions.ARB_point_sprite) {
                   GLuint u;
                   for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
                      _mesa_TexEnvi(GL_POINT_SPRITE_NV, GL_COORD_REPLACE_NV,
-                                   (GLint) point->CoordReplace[u]);
+                                   !!(point->CoordReplace & (1u << u)));
                   }
                   _mesa_set_enable(ctx, GL_POINT_SPRITE_NV,point->PointSprite);
                   if (ctx->Extensions.NV_point_sprite)
                      _mesa_PointParameteri(GL_POINT_SPRITE_R_MODE_NV,
                                            ctx->Point.SpriteRMode);
-                  _mesa_PointParameterf(GL_POINT_SPRITE_COORD_ORIGIN,
-                                        (GLfloat)ctx->Point.SpriteOrigin);
+
+                  if ((ctx->API == API_OPENGL_COMPAT && ctx->Version >= 20)
+                      || ctx->API == API_OPENGL_CORE)
+                     _mesa_PointParameterf(GL_POINT_SPRITE_COORD_ORIGIN,
+                                           (GLfloat)ctx->Point.SpriteOrigin);
                }
             }
             break;
@@ -1175,8 +1339,10 @@ _mesa_PopAttrib(void)
                _mesa_FrontFace(polygon->FrontFace);
                _mesa_PolygonMode(GL_FRONT, polygon->FrontMode);
                _mesa_PolygonMode(GL_BACK, polygon->BackMode);
-               _mesa_PolygonOffset(polygon->OffsetFactor,
-                                   polygon->OffsetUnits);
+               _mesa_polygon_offset_clamp(ctx,
+                                          polygon->OffsetFactor,
+                                          polygon->OffsetUnits,
+                                          polygon->OffsetClamp);
                _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, polygon->SmoothFlag);
                _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, polygon->StippleFlag);
                _mesa_set_enable(ctx, GL_CULL_FACE, polygon->CullFlag);
@@ -1188,19 +1354,39 @@ _mesa_PopAttrib(void)
                                 polygon->OffsetFill);
             }
             break;
-        case GL_POLYGON_STIPPLE_BIT:
-           memcpy( ctx->PolygonStipple, attr->data, 32*sizeof(GLuint) );
-           ctx->NewState |= _NEW_POLYGONSTIPPLE;
-           if (ctx->Driver.PolygonStipple)
-              ctx->Driver.PolygonStipple( ctx, (const GLubyte *) attr->data );
-           break;
+         case GL_POLYGON_STIPPLE_BIT:
+            memcpy(ctx->PolygonStipple, attr->data, 32*sizeof(GLuint));
+
+            if (ctx->DriverFlags.NewPolygonStipple)
+               ctx->NewDriverState |= ctx->DriverFlags.NewPolygonStipple;
+            else
+               ctx->NewState |= _NEW_POLYGONSTIPPLE;
+
+            if (ctx->Driver.PolygonStipple)
+               ctx->Driver.PolygonStipple(ctx, (const GLubyte *) attr->data);
+            break;
          case GL_SCISSOR_BIT:
             {
+               unsigned i;
                const struct gl_scissor_attrib *scissor;
                scissor = (const struct gl_scissor_attrib *) attr->data;
-               _mesa_Scissor(scissor->X, scissor->Y,
-                             scissor->Width, scissor->Height);
-               _mesa_set_enable(ctx, GL_SCISSOR_TEST, scissor->Enabled);
+
+               for (i = 0; i < ctx->Const.MaxViewports; i++) {
+                  _mesa_set_scissor(ctx, i,
+                                    scissor->ScissorArray[i].X,
+                                    scissor->ScissorArray[i].Y,
+                                    scissor->ScissorArray[i].Width,
+                                    scissor->ScissorArray[i].Height);
+                  _mesa_set_enablei(ctx, GL_SCISSOR_TEST, i,
+                                    (scissor->EnableFlags >> i) & 1);
+               }
+               if (ctx->Extensions.EXT_window_rectangles) {
+                  STATIC_ASSERT(sizeof(struct gl_scissor_rect) ==
+                                4 * sizeof(GLint));
+                  _mesa_WindowRectanglesEXT(
+                        scissor->WindowRectMode, scissor->NumWindowRects,
+                        (const GLint *)scissor->WindowRects);
+               }
             }
             break;
          case GL_STENCIL_BUFFER_BIT:
@@ -1242,7 +1428,7 @@ _mesa_PopAttrib(void)
                xform = (const struct gl_transform_attrib *) attr->data;
                _mesa_MatrixMode(xform->MatrixMode);
                if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
-                  _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
+                  _math_matrix_analyse(ctx->ProjectionMatrixStack.Top);
 
                /* restore clip planes */
                for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
@@ -1252,7 +1438,7 @@ _mesa_PopAttrib(void)
                   _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i,
                                    !!(xform->ClipPlanesEnabled & mask));
                   if (ctx->Driver.ClipPlane)
-                     ctx->Driver.ClipPlane( ctx, GL_CLIP_PLANE0 + i, eyePlane );
+                     ctx->Driver.ClipPlane(ctx, GL_CLIP_PLANE0 + i, eyePlane);
                }
 
                /* normalize/rescale */
@@ -1261,9 +1447,25 @@ _mesa_PopAttrib(void)
                if (xform->RescaleNormals != ctx->Transform.RescaleNormals)
                   _mesa_set_enable(ctx, GL_RESCALE_NORMAL_EXT,
                                    ctx->Transform.RescaleNormals);
-               if (xform->DepthClamp != ctx->Transform.DepthClamp)
-                  _mesa_set_enable(ctx, GL_DEPTH_CLAMP,
-                                   ctx->Transform.DepthClamp);
+
+               if (!ctx->Extensions.AMD_depth_clamp_separate) {
+                  if (xform->DepthClampNear != ctx->Transform.DepthClampNear &&
+                      xform->DepthClampFar != ctx->Transform.DepthClampFar) {
+                     _mesa_set_enable(ctx, GL_DEPTH_CLAMP,
+                                      ctx->Transform.DepthClampNear &&
+                                      ctx->Transform.DepthClampFar);
+                  }
+               } else {
+                  if (xform->DepthClampNear != ctx->Transform.DepthClampNear)
+                     _mesa_set_enable(ctx, GL_DEPTH_CLAMP_NEAR_AMD,
+                                      ctx->Transform.DepthClampNear);
+                  if (xform->DepthClampFar != ctx->Transform.DepthClampFar)
+                     _mesa_set_enable(ctx, GL_DEPTH_CLAMP_FAR_AMD,
+                                      ctx->Transform.DepthClampFar);
+               }
+
+               if (ctx->Extensions.ARB_clip_control)
+                  _mesa_ClipControl(xform->ClipOrigin, xform->ClipDepthMode);
             }
             break;
          case GL_TEXTURE_BIT:
@@ -1271,15 +1473,27 @@ _mesa_PopAttrib(void)
                struct texture_state *texstate
                   = (struct texture_state *) attr->data;
                pop_texture_group(ctx, texstate);
-              ctx->NewState |= _NEW_TEXTURE;
+               ctx->NewState |= _NEW_TEXTURE_OBJECT | _NEW_TEXTURE_STATE;
             }
             break;
          case GL_VIEWPORT_BIT:
             {
-               const struct gl_viewport_attrib *vp;
-               vp = (const struct gl_viewport_attrib *) attr->data;
-               _mesa_Viewport(vp->X, vp->Y, vp->Width, vp->Height);
-               _mesa_DepthRange(vp->Near, vp->Far);
+               unsigned i;
+               const struct viewport_state *viewstate;
+               viewstate = (const struct viewport_state *) attr->data;
+
+               for (i = 0; i < ctx->Const.MaxViewports; i++) {
+                  const struct gl_viewport_attrib *vp = &viewstate->ViewportArray[i];
+                  _mesa_set_viewport(ctx, i, vp->X, vp->Y, vp->Width,
+                                     vp->Height);
+                  _mesa_set_depth_range(ctx, i, vp->Near, vp->Far);
+               }
+
+               if (ctx->Extensions.NV_conservative_raster) {
+                  GLuint biasx = viewstate->SubpixelPrecisionBias[0];
+                  GLuint biasy = viewstate->SubpixelPrecisionBias[1];
+                  _mesa_SubpixelPrecisionBiasNV(biasx, biasy);
+               }
             }
             break;
          case GL_MULTISAMPLE_BIT_ARB:
@@ -1287,30 +1501,31 @@ _mesa_PopAttrib(void)
                const struct gl_multisample_attrib *ms;
                ms = (const struct gl_multisample_attrib *) attr->data;
 
-              TEST_AND_UPDATE(ctx->Multisample.Enabled,
-                              ms->Enabled,
-                              GL_MULTISAMPLE);
+               TEST_AND_UPDATE(ctx->Multisample.Enabled,
+                               ms->Enabled,
+                               GL_MULTISAMPLE);
 
-              TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
-                              ms->SampleCoverage,
-                              GL_SAMPLE_COVERAGE);
+               TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
+                               ms->SampleCoverage,
+                               GL_SAMPLE_COVERAGE);
 
-              TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
-                              ms->SampleAlphaToCoverage,
-                              GL_SAMPLE_ALPHA_TO_COVERAGE);
+               TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
+                               ms->SampleAlphaToCoverage,
+                               GL_SAMPLE_ALPHA_TO_COVERAGE);
 
-              TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
-                              ms->SampleAlphaToOne,
-                              GL_SAMPLE_ALPHA_TO_ONE);
+               TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
+                               ms->SampleAlphaToOne,
+                               GL_SAMPLE_ALPHA_TO_ONE);
 
-               _mesa_SampleCoverageARB(ms->SampleCoverageValue,
+               _mesa_SampleCoverage(ms->SampleCoverageValue,
                                        ms->SampleCoverageInvert);
+
+               _mesa_AlphaToCoverageDitherControlNV(ms->SampleAlphaToCoverageDitherControl);
             }
             break;
 
          default:
-            _mesa_problem( ctx, "Bad attrib flag in PopAttrib");
-            break;
+            unreachable("Bad attrib flag in PopAttrib");
       }
 
       next = attr->next;
@@ -1347,28 +1562,35 @@ copy_pixelstore(struct gl_context *ctx,
 #define GL_CLIENT_UNPACK_BIT (1<<21)
 
 /**
- * Copy gl_array_object from src to dest.
+ * Copy gl_vertex_array_object from src to dest.
  * 'dest' must be in an initialized state.
  */
 static void
 copy_array_object(struct gl_context *ctx,
-                  struct gl_array_object *dest,
-                  struct gl_array_object *src)
+                  struct gl_vertex_array_object *dest,
+                  struct gl_vertex_array_object *src)
 {
    GLuint i;
 
    /* skip Name */
    /* skip RefCount */
 
-   /* In theory must be the same anyway, but on recreate make sure it matches */
-   dest->ARBsemantics = src->ARBsemantics;
-
-   for (i = 0; i < Elements(src->VertexAttrib); i++)
-      _mesa_copy_client_array(ctx, &dest->VertexAttrib[i], &src->VertexAttrib[i]);
+   for (i = 0; i < ARRAY_SIZE(src->VertexAttrib); i++) {
+      _mesa_copy_vertex_attrib_array(ctx, &dest->VertexAttrib[i], &src->VertexAttrib[i]);
+      _mesa_copy_vertex_buffer_binding(ctx, &dest->BufferBinding[i], &src->BufferBinding[i]);
+   }
 
-   /* _Enabled must be the same than on push */
-   dest->_Enabled = src->_Enabled;
-   dest->_MaxElement = src->_MaxElement;
+   /* Enabled must be the same than on push */
+   dest->Enabled = src->Enabled;
+   dest->_EffEnabledVBO = src->_EffEnabledVBO;
+   dest->_EffEnabledNonZeroDivisor = src->_EffEnabledNonZeroDivisor;
+   /* The bitmask of bound VBOs needs to match the VertexBinding array */
+   dest->VertexAttribBufferMask = src->VertexAttribBufferMask;
+   dest->NonZeroDivisorMask = src->NonZeroDivisorMask;
+   dest->_AttributeMapMode = src->_AttributeMapMode;
+   dest->NewArrays = src->NewArrays;
+   dest->NumUpdates = src->NumUpdates;
+   dest->IsDynamic = src->IsDynamic;
 }
 
 /**
@@ -1387,15 +1609,21 @@ copy_array_attrib(struct gl_context *ctx,
    dest->LockFirst = src->LockFirst;
    dest->LockCount = src->LockCount;
    dest->PrimitiveRestart = src->PrimitiveRestart;
+   dest->PrimitiveRestartFixedIndex = src->PrimitiveRestartFixedIndex;
+   dest->_PrimitiveRestart = src->_PrimitiveRestart;
    dest->RestartIndex = src->RestartIndex;
+   memcpy(dest->_RestartIndex, src->_RestartIndex, sizeof(src->_RestartIndex));
    /* skip NewState */
    /* skip RebindArrays */
 
    if (!vbo_deleted)
-      copy_array_object(ctx, dest->ArrayObj, src->ArrayObj);
+      copy_array_object(ctx, dest->VAO, src->VAO);
 
    /* skip ArrayBufferObj */
-   /* skip ElementArrayBufferObj */
+   /* skip IndexBufferObj */
+
+   /* Invalidate array state. It will be updated during the next draw. */
+   _mesa_set_draw_vao(ctx, ctx->Array._EmptyVAO, 0);
 }
 
 /**
@@ -1408,15 +1636,15 @@ save_array_attrib(struct gl_context *ctx,
 {
    /* Set the Name, needed for restore, but do never overwrite.
     * Needs to match value in the object hash. */
-   dest->ArrayObj->Name = src->ArrayObj->Name;
+   dest->VAO->Name = src->VAO->Name;
    /* And copy all of the rest. */
    copy_array_attrib(ctx, dest, src, false);
 
    /* Just reference them here */
    _mesa_reference_buffer_object(ctx, &dest->ArrayBufferObj,
                                  src->ArrayBufferObj);
-   _mesa_reference_buffer_object(ctx, &dest->ArrayObj->ElementArrayBufferObj,
-                                 src->ArrayObj->ElementArrayBufferObj);
+   _mesa_reference_buffer_object(ctx, &dest->VAO->IndexBufferObj,
+                                 src->VAO->IndexBufferObj);
 }
 
 /**
@@ -1427,6 +1655,8 @@ restore_array_attrib(struct gl_context *ctx,
                      struct gl_array_attrib *dest,
                      struct gl_array_attrib *src)
 {
+   bool is_vao_name_zero = src->VAO->Name == 0;
+
    /* The ARB_vertex_array_object spec says:
     *
     *     "BindVertexArray fails and an INVALID_OPERATION error is generated
@@ -1435,181 +1665,179 @@ restore_array_attrib(struct gl_context *ctx,
     *     DeleteVertexArrays."
     *
     * Therefore popping a deleted VAO cannot magically recreate it.
-    *
-    * The semantics of objects created using APPLE_vertex_array_objects behave
-    * differently.  These objects expect to be recreated by pop.  Alas.
     */
-   const bool arb_vao = (src->ArrayObj->Name != 0
-                        && src->ArrayObj->ARBsemantics);
-
-   if (arb_vao && !_mesa_IsVertexArrayAPPLE(src->ArrayObj->Name))
+   if (!is_vao_name_zero && !_mesa_IsVertexArray(src->VAO->Name))
       return;
 
-   _mesa_BindVertexArrayAPPLE(src->ArrayObj->Name);
+   _mesa_BindVertexArray(src->VAO->Name);
 
    /* Restore or recreate the buffer objects by the names ... */
-   if (!arb_vao
-       || src->ArrayBufferObj->Name == 0
-       || _mesa_IsBufferARB(src->ArrayBufferObj->Name)) {
+   if (is_vao_name_zero || !src->ArrayBufferObj ||
+       _mesa_IsBuffer(src->ArrayBufferObj->Name)) {
       /* ... and restore its content */
       copy_array_attrib(ctx, dest, src, false);
 
-      _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB,
-                         src->ArrayBufferObj->Name);
+      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB,
+                       src->ArrayBufferObj ?
+                          src->ArrayBufferObj->Name : 0);
    } else {
       copy_array_attrib(ctx, dest, src, true);
    }
 
-   if (!arb_vao
-       || src->ArrayObj->ElementArrayBufferObj->Name == 0
-       || _mesa_IsBufferARB(src->ArrayObj->ElementArrayBufferObj->Name))
-      _mesa_BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
-                         src->ArrayObj->ElementArrayBufferObj->Name);
-}
-
-/**
- * init/alloc the fields of 'attrib'.
- * Needs to the init part matching free_array_attrib_data below.
- */
-static void
-init_array_attrib_data(struct gl_context *ctx,
-                       struct gl_array_attrib *attrib)
-{
-   /* Get a non driver gl_array_object. */
-   attrib->ArrayObj = CALLOC_STRUCT( gl_array_object );
-   _mesa_initialize_array_object(ctx, attrib->ArrayObj, 0);
-}
-
-/**
- * Free/unreference the fields of 'attrib' but don't delete it (that's
- * done later in the calling code).
- * Needs to the cleanup part matching init_array_attrib_data above.
- */
-static void
-free_array_attrib_data(struct gl_context *ctx,
-                       struct gl_array_attrib *attrib)
-{
-   /* We use a non driver array object, so don't just unref since we would
-    * end up using the drivers DeleteArrayObject function for deletion. */
-   _mesa_delete_array_object(ctx, attrib->ArrayObj);
-   attrib->ArrayObj = 0;
-   _mesa_reference_buffer_object(ctx, &attrib->ArrayBufferObj, NULL);
+   if (is_vao_name_zero || !src->VAO->IndexBufferObj ||
+       _mesa_IsBuffer(src->VAO->IndexBufferObj->Name)) {
+      _mesa_BindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,
+                       src->VAO->IndexBufferObj ?
+                          src->VAO->IndexBufferObj->Name : 0);
+   }
 }
 
 
 void GLAPIENTRY
 _mesa_PushClientAttrib(GLbitfield mask)
 {
-   struct gl_attrib_node *head;
+   struct gl_client_attrib_node *head;
 
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
-      _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushClientAttrib" );
+      _mesa_error(ctx, GL_STACK_OVERFLOW, "glPushClientAttrib");
       return;
    }
 
-   /* Build linked list of attribute nodes which save all attribute
-    * groups specified by the mask.
-    */
-   head = NULL;
+   head = &ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
+   head->Mask = mask;
 
    if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
-      struct gl_pixelstore_attrib *attr;
-      /* packing attribs */
-      attr = CALLOC_STRUCT( gl_pixelstore_attrib );
-      copy_pixelstore(ctx, attr, &ctx->Pack);
-      save_attrib_data(&head, GL_CLIENT_PACK_BIT, attr);
-      /* unpacking attribs */
-      attr = CALLOC_STRUCT( gl_pixelstore_attrib );
-      copy_pixelstore(ctx, attr, &ctx->Unpack);
-      save_attrib_data(&head, GL_CLIENT_UNPACK_BIT, attr);
+      copy_pixelstore(ctx, &head->Pack, &ctx->Pack);
+      copy_pixelstore(ctx, &head->Unpack, &ctx->Unpack);
    }
 
    if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
-      struct gl_array_attrib *attr;
-      attr = CALLOC_STRUCT( gl_array_attrib );
-      init_array_attrib_data(ctx, attr);
-      save_array_attrib(ctx, attr, &ctx->Array);
-      save_attrib_data(&head, GL_CLIENT_VERTEX_ARRAY_BIT, attr);
+      _mesa_initialize_vao(ctx, &head->VAO, 0);
+      /* Use the VAO declared within the node instead of allocating it. */
+      head->Array.VAO = &head->VAO;
+      save_array_attrib(ctx, &head->Array, &ctx->Array);
    }
 
-   ctx->ClientAttribStack[ctx->ClientAttribStackDepth] = head;
    ctx->ClientAttribStackDepth++;
 }
 
 
-
-
 void GLAPIENTRY
 _mesa_PopClientAttrib(void)
 {
-   struct gl_attrib_node *node, *next;
+   struct gl_client_attrib_node *head;
 
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+   FLUSH_VERTICES(ctx, 0);
 
    if (ctx->ClientAttribStackDepth == 0) {
-      _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib" );
+      _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib");
       return;
    }
 
    ctx->ClientAttribStackDepth--;
-   node = ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
+   head = &ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
 
-   while (node) {
-      switch (node->kind) {
-         case GL_CLIENT_PACK_BIT:
-            {
-               struct gl_pixelstore_attrib *store =
-                  (struct gl_pixelstore_attrib *) node->data;
-               copy_pixelstore(ctx, &ctx->Pack, store);
-               _mesa_reference_buffer_object(ctx, &store->BufferObj, NULL);
-            }
-           ctx->NewState |= _NEW_PACKUNPACK;
-            break;
-         case GL_CLIENT_UNPACK_BIT:
-            {
-               struct gl_pixelstore_attrib *store =
-                  (struct gl_pixelstore_attrib *) node->data;
-               copy_pixelstore(ctx, &ctx->Unpack, store);
-               _mesa_reference_buffer_object(ctx, &store->BufferObj, NULL);
-            }
-           ctx->NewState |= _NEW_PACKUNPACK;
-            break;
-         case GL_CLIENT_VERTEX_ARRAY_BIT: {
-           struct gl_array_attrib * attr =
-             (struct gl_array_attrib *) node->data;
-            restore_array_attrib(ctx, &ctx->Array, attr);
-            free_array_attrib_data(ctx, attr);
-           ctx->NewState |= _NEW_ARRAY;
-            break;
-        }
-         default:
-            _mesa_problem( ctx, "Bad attrib flag in PopClientAttrib");
-            break;
-      }
+   if (head->Mask & GL_CLIENT_PIXEL_STORE_BIT) {
+      copy_pixelstore(ctx, &ctx->Pack, &head->Pack);
+      _mesa_reference_buffer_object(ctx, &head->Pack.BufferObj, NULL);
 
-      next = node->next;
-      free(node->data);
-      free(node);
-      node = next;
+      copy_pixelstore(ctx, &ctx->Unpack, &head->Unpack);
+      _mesa_reference_buffer_object(ctx, &head->Unpack.BufferObj, NULL);
    }
-}
 
+   if (head->Mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
+      restore_array_attrib(ctx, &ctx->Array, &head->Array);
+      _mesa_unbind_array_object_vbos(ctx, &head->VAO);
+      _mesa_reference_buffer_object(ctx, &head->VAO.IndexBufferObj, NULL);
+      _mesa_reference_buffer_object(ctx, &head->Array.ArrayBufferObj, NULL);
+   }
+}
 
-void
-_mesa_init_attrib_dispatch(struct _glapi_table *disp)
+void GLAPIENTRY
+_mesa_ClientAttribDefaultEXT( GLbitfield mask )
 {
-   SET_PopAttrib(disp, _mesa_PopAttrib);
-   SET_PushAttrib(disp, _mesa_PushAttrib);
-   SET_PopClientAttrib(disp, _mesa_PopClientAttrib);
-   SET_PushClientAttrib(disp, _mesa_PushClientAttrib);
-}
+   if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
+      _mesa_PixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
+      _mesa_PixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
+      _mesa_PixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
+      _mesa_PixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
+      _mesa_PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
+      _mesa_PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
+      _mesa_PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
+      _mesa_PixelStorei(GL_UNPACK_ALIGNMENT, 4);
+      _mesa_PixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
+      _mesa_PixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
+      _mesa_PixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
+      _mesa_PixelStorei(GL_PACK_SKIP_IMAGES, 0);
+      _mesa_PixelStorei(GL_PACK_ROW_LENGTH, 0);
+      _mesa_PixelStorei(GL_PACK_SKIP_ROWS, 0);
+      _mesa_PixelStorei(GL_PACK_SKIP_PIXELS, 0);
+      _mesa_PixelStorei(GL_PACK_ALIGNMENT, 4);
+
+      _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
+      _mesa_BindBuffer(GL_PIXEL_PACK_BUFFER, 0);
+   }
+   if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
+      GET_CURRENT_CONTEXT(ctx);
+      int i;
+
+      _mesa_BindBuffer(GL_ARRAY_BUFFER, 0);
+      _mesa_BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+
+      _mesa_DisableClientState(GL_EDGE_FLAG_ARRAY);
+      _mesa_EdgeFlagPointer(0, 0);
+
+      _mesa_DisableClientState(GL_INDEX_ARRAY);
+      _mesa_IndexPointer(GL_FLOAT, 0, 0);
 
+      _mesa_DisableClientState(GL_SECONDARY_COLOR_ARRAY);
+      _mesa_SecondaryColorPointer(4, GL_FLOAT, 0, 0);
 
-#endif /* FEATURE_attrib_stack */
+      _mesa_DisableClientState(GL_FOG_COORD_ARRAY);
+      _mesa_FogCoordPointer(GL_FLOAT, 0, 0);
+
+      for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
+         _mesa_ClientActiveTexture(GL_TEXTURE0 + i);
+         _mesa_DisableClientState(GL_TEXTURE_COORD_ARRAY);
+         _mesa_TexCoordPointer(4, GL_FLOAT, 0, 0);
+      }
+
+      _mesa_DisableClientState(GL_COLOR_ARRAY);
+      _mesa_ColorPointer(4, GL_FLOAT, 0, 0);
+
+      _mesa_DisableClientState(GL_NORMAL_ARRAY);
+      _mesa_NormalPointer(GL_FLOAT, 0, 0);
+
+      _mesa_DisableClientState(GL_VERTEX_ARRAY);
+      _mesa_VertexPointer(4, GL_FLOAT, 0, 0);
+
+      for (i = 0; i < ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs; i++) {
+         _mesa_DisableVertexAttribArray(i);
+         _mesa_VertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, 0, 0);
+      }
+
+      _mesa_ClientActiveTexture(GL_TEXTURE0);
+
+      _mesa_PrimitiveRestartIndex_no_error(0);
+      if (ctx->Version >= 31)
+         _mesa_Disable(GL_PRIMITIVE_RESTART);
+      else if (_mesa_has_NV_primitive_restart(ctx))
+         _mesa_DisableClientState(GL_PRIMITIVE_RESTART_NV);
+
+      if (_mesa_has_ARB_ES3_compatibility(ctx))
+         _mesa_Disable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
+   }
+}
+
+void GLAPIENTRY
+_mesa_PushClientAttribDefaultEXT( GLbitfield mask )
+{
+   _mesa_PushClientAttrib(mask);
+   _mesa_ClientAttribDefaultEXT(mask);
+}
 
 
 /**
@@ -1649,7 +1877,8 @@ _mesa_free_attrib_data(struct gl_context *ctx)
 }
 
 
-void _mesa_init_attrib( struct gl_context *ctx )
+void
+_mesa_init_attrib(struct gl_context *ctx)
 {
    /* Renderer and client attribute stacks */
    ctx->AttribStackDepth = 0;