need to copy new 1D/2D array texture objects in _mesa_PushAttrib()
[mesa.git] / src / mesa / main / attrib.c
index e669ff11d59d28dec48bb9d4c68589e6ad7bdc18..4699546262947cd24366f201a1474f155da512ec 100644 (file)
@@ -1,10 +1,8 @@
-/* $Id: attrib.c,v 1.40 2001/01/23 23:39:36 brianp Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
+ * Version:  6.5.3
  *
- * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
-
-#ifdef PC_HEADER
-#include "all.h"
-#else
 #include "glheader.h"
+#include "imports.h"
+#include "accum.h"
+#include "arrayobj.h"
 #include "attrib.h"
+#include "blend.h"
 #include "buffers.h"
+#include "bufferobj.h"
+#include "colormac.h"
+#include "colortab.h"
 #include "context.h"
+#include "depth.h"
 #include "enable.h"
 #include "enums.h"
+#include "fog.h"
+#include "hint.h"
+#include "light.h"
+#include "lines.h"
 #include "matrix.h"
-#include "mem.h"
+#include "points.h"
+#include "polygon.h"
 #include "simple_list.h"
+#include "stencil.h"
+#include "texobj.h"
 #include "texstate.h"
 #include "mtypes.h"
-#endif
-
-
+#include "math/m_xform.h"
 
 
-/*
+/**
  * Allocate a new attribute state node.  These nodes have a
  * "kind" value and a pointer to a struct of state data.
  */
@@ -59,41 +66,7 @@ new_attrib_node( GLbitfield kind )
 }
 
 
-
-/*
- * Copy texture object state from one texture object to another.
- */
-static void
-copy_texobj_state( struct gl_texture_object *dest,
-                   const struct gl_texture_object *src )
-{
-   /*
-   dest->Name = src->Name;
-   dest->Dimensions = src->Dimensions;
-   */
-   dest->Priority = src->Priority;
-   dest->BorderColor[0] = src->BorderColor[0];
-   dest->BorderColor[1] = src->BorderColor[1];
-   dest->BorderColor[2] = src->BorderColor[2];
-   dest->BorderColor[3] = src->BorderColor[3];
-   dest->WrapS = src->WrapS;
-   dest->WrapT = src->WrapT;
-   dest->WrapR = src->WrapR;
-   dest->MinFilter = src->MinFilter;
-   dest->MagFilter = src->MagFilter;
-   dest->MinLod = src->MinLod;
-   dest->MaxLod = src->MaxLod;
-   dest->BaseLevel = src->BaseLevel;
-   dest->MaxLevel = src->MaxLevel;
-   dest->_MaxLevel = src->_MaxLevel;
-   dest->_MaxLambda = src->_MaxLambda;
-   dest->Palette = src->Palette;
-   dest->Complete = src->Complete;
-}
-
-
-
-void
+void GLAPIENTRY
 _mesa_PushAttrib(GLbitfield mask)
 {
    struct gl_attrib_node *newnode;
@@ -102,11 +75,11 @@ _mesa_PushAttrib(GLbitfield mask)
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   if (MESA_VERBOSE&VERBOSE_API)
-      fprintf(stderr, "glPushAttrib %x\n", (int)mask);
+   if (MESA_VERBOSE & VERBOSE_API)
+      _mesa_debug(ctx, "glPushAttrib %x\n", (int) mask);
 
-   if (ctx->AttribStackDepth>=MAX_ATTRIB_STACK_DEPTH) {
-      gl_error( ctx, GL_STACK_OVERFLOW, "glPushAttrib" );
+   if (ctx->AttribStackDepth >= MAX_ATTRIB_STACK_DEPTH) {
+      _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushAttrib" );
       return;
    }
 
@@ -163,10 +136,11 @@ _mesa_PushAttrib(GLbitfield mask)
       attr->AlphaTest = ctx->Color.AlphaEnabled;
       attr->AutoNormal = ctx->Eval.AutoNormal;
       attr->Blend = ctx->Color.BlendEnabled;
-      for (i=0;i<MAX_CLIP_PLANES;i++) {
-         attr->ClipPlane[i] = ctx->Transform.ClipEnabled[i];
-      }
+      attr->ClipPlanes = ctx->Transform.ClipPlanesEnabled;
       attr->ColorMaterial = ctx->Light.ColorMaterialEnabled;
+      for (i = 0; i < COLORTABLE_MAX; i++) {
+         attr->ColorTable[i] = ctx->Pixel.ColorTableEnabled[i];
+      }
       attr->Convolution1D = ctx->Pixel.Convolution1DEnabled;
       attr->Convolution2D = ctx->Pixel.Convolution2DEnabled;
       attr->Separable2D = ctx->Pixel.Separable2DEnabled;
@@ -174,7 +148,7 @@ _mesa_PushAttrib(GLbitfield mask)
       attr->DepthTest = ctx->Depth.Test;
       attr->Dither = ctx->Color.DitherFlag;
       attr->Fog = ctx->Fog.Enabled;
-      for (i=0;i<MAX_LIGHTS;i++) {
+      for (i = 0; i < ctx->Const.MaxLights; i++) {
          attr->Light[i] = ctx->Light.Light[i].Enabled;
       }
       attr->Lighting = ctx->Light.Enabled;
@@ -193,6 +167,7 @@ _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;
@@ -202,9 +177,11 @@ _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->PixelTexture = ctx->Pixel.PixelTextureEnabled;
+      attr->RasterPositionUnclipped = ctx->Transform.RasterPositionUnclipped;
       attr->PointSmooth = ctx->Point.SmoothFlag;
+      attr->PointSprite = ctx->Point.PointSprite;
       attr->PolygonOffsetPoint = ctx->Polygon.OffsetPoint;
       attr->PolygonOffsetLine = ctx->Polygon.OffsetLine;
       attr->PolygonOffsetFill = ctx->Polygon.OffsetFill;
@@ -213,10 +190,21 @@ _mesa_PushAttrib(GLbitfield mask)
       attr->RescaleNormals = ctx->Transform.RescaleNormals;
       attr->Scissor = ctx->Scissor.Enabled;
       attr->Stencil = ctx->Stencil.Enabled;
+      attr->StencilTwoSide = ctx->Stencil.TestTwoSide;
+      attr->MultisampleEnabled = ctx->Multisample.Enabled;
+      attr->SampleAlphaToCoverage = ctx->Multisample.SampleAlphaToCoverage;
+      attr->SampleAlphaToOne = ctx->Multisample.SampleAlphaToOne;
+      attr->SampleCoverage = ctx->Multisample.SampleCoverage;
+      attr->SampleCoverageInvert = ctx->Multisample.SampleCoverageInvert;
       for (i=0; i<MAX_TEXTURE_UNITS; i++) {
          attr->Texture[i] = ctx->Texture.Unit[i].Enabled;
          attr->TexGen[i] = ctx->Texture.Unit[i].TexGenEnabled;
+         attr->TextureColorTable[i] = ctx->Texture.Unit[i].ColorTableEnabled;
       }
+      /* GL_NV_vertex_program */
+      attr->VertexProgram = ctx->VertexProgram.Enabled;
+      attr->VertexProgramPointSize = ctx->VertexProgram.PointSizeEnabled;
+      attr->VertexProgramTwoSide = ctx->VertexProgram.TwoSideEnabled;
       newnode = new_attrib_node( GL_ENABLE_BIT );
       newnode->data = attr;
       newnode->next = head;
@@ -288,6 +276,8 @@ _mesa_PushAttrib(GLbitfield mask)
       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;
       newnode = new_attrib_node( GL_PIXEL_MODE_BIT );
       newnode->data = attr;
       newnode->next = head;
@@ -347,22 +337,40 @@ _mesa_PushAttrib(GLbitfield mask)
    if (mask & GL_TEXTURE_BIT) {
       struct gl_texture_attrib *attr;
       GLuint u;
-      /* Take care of texture object reference counters */
+
+      _mesa_lock_context_textures(ctx);
+      /* Bump the texture object reference counts so that they don't
+       * inadvertantly get deleted.
+       */
       for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
         ctx->Texture.Unit[u].Current1D->RefCount++;
         ctx->Texture.Unit[u].Current2D->RefCount++;
         ctx->Texture.Unit[u].Current3D->RefCount++;
         ctx->Texture.Unit[u].CurrentCubeMap->RefCount++;
+        ctx->Texture.Unit[u].CurrentRect->RefCount++;
       }
       attr = MALLOC_STRUCT( gl_texture_attrib );
       MEMCPY( attr, &ctx->Texture, sizeof(struct gl_texture_attrib) );
       /* copy state of the currently bound texture objects */
       for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
-         copy_texobj_state(&attr->Unit[u].Saved1D, attr->Unit[u].Current1D);
-         copy_texobj_state(&attr->Unit[u].Saved2D, attr->Unit[u].Current2D);
-         copy_texobj_state(&attr->Unit[u].Saved3D, attr->Unit[u].Current3D);
-         copy_texobj_state(&attr->Unit[u].SavedCubeMap, attr->Unit[u].CurrentCubeMap);
+         _mesa_copy_texture_object(&attr->Unit[u].Saved1D,
+                                   attr->Unit[u].Current1D);
+         _mesa_copy_texture_object(&attr->Unit[u].Saved2D,
+                                   attr->Unit[u].Current2D);
+         _mesa_copy_texture_object(&attr->Unit[u].Saved3D,
+                                   attr->Unit[u].Current3D);
+         _mesa_copy_texture_object(&attr->Unit[u].SavedCubeMap,
+                                   attr->Unit[u].CurrentCubeMap);
+         _mesa_copy_texture_object(&attr->Unit[u].SavedRect,
+                                   attr->Unit[u].CurrentRect);
+         _mesa_copy_texture_object(&attr->Unit[u].Saved1DArray,
+                                   attr->Unit[u].Current1DArray);
+         _mesa_copy_texture_object(&attr->Unit[u].Saved2DArray,
+                                   attr->Unit[u].Current2DArray);
       }
+
+      _mesa_unlock_context_textures(ctx);
+
       newnode = new_attrib_node( GL_TEXTURE_BIT );
       newnode->data = attr;
       newnode->next = head;
@@ -389,6 +397,17 @@ _mesa_PushAttrib(GLbitfield mask)
       head = newnode;
    }
 
+   /* 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) );
+      newnode = new_attrib_node( GL_MULTISAMPLE_BIT_ARB );
+      newnode->data = attr;
+      newnode->next = head;
+      head = newnode;
+   }
+
    ctx->AttribStack[ctx->AttribStackDepth] = head;
    ctx->AttribStackDepth++;
 }
@@ -406,17 +425,26 @@ pop_enable_group(GLcontext *ctx, const struct gl_enable_attrib *enable)
        }
 
    TEST_AND_UPDATE(ctx->Color.AlphaEnabled, enable->AlphaTest, GL_ALPHA_TEST);
-   TEST_AND_UPDATE(ctx->Transform.Normalize, enable->AutoNormal, GL_NORMALIZE);
    TEST_AND_UPDATE(ctx->Color.BlendEnabled, enable->Blend, GL_BLEND);
 
    for (i=0;i<MAX_CLIP_PLANES;i++) {
-      if (ctx->Transform.ClipEnabled[i] != enable->ClipPlane[i])
-         _mesa_set_enable(ctx, (GLenum) (GL_CLIP_PLANE0 + i),
-                          enable->ClipPlane[i]);
+      const GLuint mask = 1 << i;
+      if ((ctx->Transform.ClipPlanesEnabled & mask) != (enable->ClipPlanes & mask))
+         _mesa_set_enable(ctx, (GLenum) (GL_CLIP_PLANE0 + i),
+                          (GLboolean) ((enable->ClipPlanes & mask) ? GL_TRUE : GL_FALSE));
    }
 
    TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled, enable->ColorMaterial,
                    GL_COLOR_MATERIAL);
+   TEST_AND_UPDATE(ctx->Pixel.ColorTableEnabled[COLORTABLE_PRECONVOLUTION],
+                   enable->ColorTable[COLORTABLE_PRECONVOLUTION],
+                   GL_COLOR_TABLE);
+   TEST_AND_UPDATE(ctx->Pixel.ColorTableEnabled[COLORTABLE_POSTCONVOLUTION],
+                   enable->ColorTable[COLORTABLE_POSTCONVOLUTION],
+                   GL_POST_CONVOLUTION_COLOR_TABLE);
+   TEST_AND_UPDATE(ctx->Pixel.ColorTableEnabled[COLORTABLE_POSTCOLORMATRIX],
+                   enable->ColorTable[COLORTABLE_POSTCOLORMATRIX],
+                   GL_POST_COLOR_MATRIX_COLOR_TABLE);
    TEST_AND_UPDATE(ctx->Polygon.CullFlag, enable->CullFace, GL_CULL_FACE);
    TEST_AND_UPDATE(ctx->Depth.Test, enable->DepthTest, GL_DEPTH_TEST);
    TEST_AND_UPDATE(ctx->Color.DitherFlag, enable->Dither, GL_DITHER);
@@ -435,6 +463,7 @@ pop_enable_group(GLcontext *ctx, const struct gl_enable_attrib *enable)
                    GL_INDEX_LOGIC_OP);
    TEST_AND_UPDATE(ctx->Color.ColorLogicOpEnabled, enable->ColorLogicOp,
                    GL_COLOR_LOGIC_OP);
+
    TEST_AND_UPDATE(ctx->Eval.Map1Color4, enable->Map1Color4, GL_MAP1_COLOR_4);
    TEST_AND_UPDATE(ctx->Eval.Map1Index, enable->Map1Index, GL_MAP1_INDEX);
    TEST_AND_UPDATE(ctx->Eval.Map1Normal, enable->Map1Normal, GL_MAP1_NORMAL);
@@ -450,6 +479,11 @@ pop_enable_group(GLcontext *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);
    TEST_AND_UPDATE(ctx->Eval.Map2Normal, enable->Map2Normal, GL_MAP2_NORMAL);
@@ -465,13 +499,24 @@ pop_enable_group(GLcontext *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);
    TEST_AND_UPDATE(ctx->Transform.RescaleNormals, enable->RescaleNormals,
                    GL_RESCALE_NORMAL_EXT);
-   TEST_AND_UPDATE(ctx->Pixel.PixelTextureEnabled, enable->PixelTexture,
-                   GL_POINT_SMOOTH);
+   TEST_AND_UPDATE(ctx->Transform.RasterPositionUnclipped,
+                   enable->RasterPositionUnclipped,
+                   GL_RASTER_POSITION_UNCLIPPED_IBM);
    TEST_AND_UPDATE(ctx->Point.SmoothFlag, enable->PointSmooth,
                    GL_POINT_SMOOTH);
+   if (ctx->Extensions.NV_point_sprite || ctx->Extensions.ARB_point_sprite) {
+      TEST_AND_UPDATE(ctx->Point.PointSprite, enable->PointSprite,
+                      GL_POINT_SPRITE_NV);
+   }
    TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, enable->PolygonOffsetPoint,
                    GL_POLYGON_OFFSET_POINT);
    TEST_AND_UPDATE(ctx->Polygon.OffsetLine, enable->PolygonOffsetLine,
@@ -484,6 +529,34 @@ pop_enable_group(GLcontext *ctx, const struct gl_enable_attrib *enable)
                    GL_POLYGON_STIPPLE);
    TEST_AND_UPDATE(ctx->Scissor.Enabled, enable->Scissor, GL_SCISSOR_TEST);
    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);
+   }
+   TEST_AND_UPDATE(ctx->Multisample.Enabled, enable->MultisampleEnabled,
+                   GL_MULTISAMPLE_ARB);
+   TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
+                   enable->SampleAlphaToCoverage,
+                   GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
+   TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
+                   enable->SampleAlphaToOne,
+                   GL_SAMPLE_ALPHA_TO_ONE_ARB);
+   TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
+                   enable->SampleCoverage,
+                   GL_SAMPLE_COVERAGE_ARB);
+   TEST_AND_UPDATE(ctx->Multisample.SampleCoverageInvert,
+                   enable->SampleCoverageInvert,
+                   GL_SAMPLE_COVERAGE_INVERT_ARB);
+   /* GL_ARB_vertex_program, GL_NV_vertex_program */
+   TEST_AND_UPDATE(ctx->VertexProgram.Enabled,
+                   enable->VertexProgram,
+                   GL_VERTEX_PROGRAM_ARB);
+   TEST_AND_UPDATE(ctx->VertexProgram.PointSizeEnabled,
+                   enable->VertexProgramPointSize,
+                   GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
+   TEST_AND_UPDATE(ctx->VertexProgram.TwoSideEnabled,
+                   enable->VertexProgramTwoSide,
+                   GL_VERTEX_PROGRAM_TWO_SIDE_ARB);
+
 #undef TEST_AND_UPDATE
 
    /* texture unit enables */
@@ -495,11 +568,17 @@ pop_enable_group(GLcontext *ctx, const struct gl_enable_attrib *enable)
                (*ctx->Driver.ActiveTexture)(ctx, i);
             }
             (*ctx->Driver.Enable)( ctx, GL_TEXTURE_1D,
-                             (GLboolean) (enable->Texture[i] & TEXTURE0_1D) );
+                             (GLboolean) (enable->Texture[i] & TEXTURE_1D_BIT) );
             (*ctx->Driver.Enable)( ctx, GL_TEXTURE_2D,
-                             (GLboolean) (enable->Texture[i] & TEXTURE0_2D) );
+                             (GLboolean) (enable->Texture[i] & TEXTURE_2D_BIT) );
             (*ctx->Driver.Enable)( ctx, GL_TEXTURE_3D,
-                             (GLboolean) (enable->Texture[i] & TEXTURE0_3D) );
+                             (GLboolean) (enable->Texture[i] & TEXTURE_3D_BIT) );
+            if (ctx->Extensions.ARB_texture_cube_map)
+               (*ctx->Driver.Enable)( ctx, GL_TEXTURE_CUBE_MAP_ARB,
+                          (GLboolean) (enable->Texture[i] & TEXTURE_CUBE_BIT) );
+            if (ctx->Extensions.NV_texture_rectangle)
+               (*ctx->Driver.Enable)( ctx, GL_TEXTURE_RECTANGLE_NV,
+                          (GLboolean) (enable->Texture[i] & TEXTURE_RECT_BIT) );
          }
       }
 
@@ -527,6 +606,9 @@ pop_enable_group(GLcontext *ctx, const struct gl_enable_attrib *enable)
                (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_Q, GL_FALSE);
          }
       }
+
+      /* GL_SGI_texture_color_table */
+      ctx->Texture.Unit[i].ColorTableEnabled = enable->TextureColorTable[i];
    }
 
    if (ctx->Driver.ActiveTexture) {
@@ -535,20 +617,225 @@ pop_enable_group(GLcontext *ctx, const struct gl_enable_attrib *enable)
 }
 
 
+static void
+pop_texture_group(GLcontext *ctx, const struct gl_texture_attrib *texAttrib)
+{
+   GLuint u;
+
+   for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
+      const struct gl_texture_unit *unit = &texAttrib->Unit[u];
+      GLuint i;
+
+      _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + u);
+      _mesa_set_enable(ctx, GL_TEXTURE_1D,
+                       (unit->Enabled & TEXTURE_1D_BIT) ? GL_TRUE : GL_FALSE);
+      _mesa_set_enable(ctx, GL_TEXTURE_2D,
+                       (unit->Enabled & TEXTURE_2D_BIT) ? GL_TRUE : GL_FALSE);
+      _mesa_set_enable(ctx, GL_TEXTURE_3D,
+                       (unit->Enabled & TEXTURE_3D_BIT) ? GL_TRUE : GL_FALSE);
+      if (ctx->Extensions.ARB_texture_cube_map) {
+         _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP_ARB,
+                     (unit->Enabled & TEXTURE_CUBE_BIT) ? GL_TRUE : GL_FALSE);
+      }
+      if (ctx->Extensions.NV_texture_rectangle) {
+         _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE_NV,
+                     (unit->Enabled & TEXTURE_RECT_BIT) ? GL_TRUE : GL_FALSE);
+      }
+      if (ctx->Extensions.SGI_texture_color_table) {
+         _mesa_set_enable(ctx, GL_TEXTURE_COLOR_TABLE_SGI,
+                          unit->ColorTableEnabled);
+      }
+      _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->GenModeS);
+      _mesa_TexGeni(GL_T, GL_TEXTURE_GEN_MODE, unit->GenModeT);
+      _mesa_TexGeni(GL_R, GL_TEXTURE_GEN_MODE, unit->GenModeR);
+      _mesa_TexGeni(GL_Q, GL_TEXTURE_GEN_MODE, unit->GenModeQ);
+      _mesa_TexGenfv(GL_S, GL_OBJECT_PLANE, unit->ObjectPlaneS);
+      _mesa_TexGenfv(GL_T, GL_OBJECT_PLANE, unit->ObjectPlaneT);
+      _mesa_TexGenfv(GL_R, GL_OBJECT_PLANE, unit->ObjectPlaneR);
+      _mesa_TexGenfv(GL_Q, GL_OBJECT_PLANE, unit->ObjectPlaneQ);
+      /* Eye plane done differently to avoid re-transformation */
+      {
+         struct gl_texture_unit *destUnit = &ctx->Texture.Unit[u];
+         COPY_4FV(destUnit->EyePlaneS, unit->EyePlaneS);
+         COPY_4FV(destUnit->EyePlaneT, unit->EyePlaneT);
+         COPY_4FV(destUnit->EyePlaneR, unit->EyePlaneR);
+         COPY_4FV(destUnit->EyePlaneQ, unit->EyePlaneQ);
+         if (ctx->Driver.TexGen) {
+            ctx->Driver.TexGen(ctx, GL_S, GL_EYE_PLANE, unit->EyePlaneS);
+            ctx->Driver.TexGen(ctx, GL_T, GL_EYE_PLANE, unit->EyePlaneT);
+            ctx->Driver.TexGen(ctx, GL_R, GL_EYE_PLANE, unit->EyePlaneR);
+            ctx->Driver.TexGen(ctx, GL_Q, GL_EYE_PLANE, unit->EyePlaneQ);
+         }
+      }
+      _mesa_set_enable(ctx, GL_TEXTURE_GEN_S,
+                       ((unit->TexGenEnabled & S_BIT) ? GL_TRUE : GL_FALSE));
+      _mesa_set_enable(ctx, GL_TEXTURE_GEN_T,
+                       ((unit->TexGenEnabled & T_BIT) ? GL_TRUE : GL_FALSE));
+      _mesa_set_enable(ctx, GL_TEXTURE_GEN_R,
+                       ((unit->TexGenEnabled & R_BIT) ? GL_TRUE : GL_FALSE));
+      _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q,
+                       ((unit->TexGenEnabled & Q_BIT) ? GL_TRUE : GL_FALSE));
+      if (ctx->Extensions.EXT_texture_lod_bias) {
+         _mesa_TexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
+                       GL_TEXTURE_LOD_BIAS_EXT, unit->LodBias);
+      }
+      if (ctx->Extensions.EXT_texture_env_combine ||
+          ctx->Extensions.ARB_texture_env_combine) {
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,
+                       unit->Combine.ModeRGB);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,
+                       unit->Combine.ModeA);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB,
+                       unit->Combine.SourceRGB[0]);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB,
+                       unit->Combine.SourceRGB[1]);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB,
+                       unit->Combine.SourceRGB[2]);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA,
+                       unit->Combine.SourceA[0]);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA,
+                       unit->Combine.SourceA[1]);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA,
+                       unit->Combine.SourceA[2]);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB,
+                       unit->Combine.OperandRGB[0]);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB,
+                       unit->Combine.OperandRGB[1]);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB,
+                       unit->Combine.OperandRGB[2]);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA,
+                       unit->Combine.OperandA[0]);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA,
+                       unit->Combine.OperandA[1]);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA,
+                       unit->Combine.OperandA[2]);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE,
+                       1 << unit->Combine.ScaleShiftRGB);
+         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE,
+                       1 << unit->Combine.ScaleShiftA);
+      }
+
+      /* Restore texture object state */
+      for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
+         GLenum target = 0;
+         const struct gl_texture_object *obj = NULL;
+         GLfloat bordColor[4];
+
+         switch (i) {
+         case 0:
+            target = GL_TEXTURE_1D;
+            obj = &unit->Saved1D;
+            break;
+         case 1:
+            target = GL_TEXTURE_2D;
+            obj = &unit->Saved2D;
+            break;
+         case 2:
+            target = GL_TEXTURE_3D;
+            obj = &unit->Saved3D;
+            break;
+         case 3:
+            if (!ctx->Extensions.ARB_texture_cube_map)
+               continue;
+            target = GL_TEXTURE_CUBE_MAP_ARB;
+            obj = &unit->SavedCubeMap;
+            break;
+         case 4:
+            if (!ctx->Extensions.NV_texture_rectangle)
+               continue;
+            target = GL_TEXTURE_RECTANGLE_NV;
+            obj = &unit->SavedRect;
+            break;
+         case 5:
+            if (!ctx->Extensions.MESA_texture_array)
+               continue;
+            target = GL_TEXTURE_1D_ARRAY_EXT;
+            obj = &unit->Saved1DArray;
+            break;
+         case 6:
+            if (!ctx->Extensions.MESA_texture_array)
+               continue;
+            target = GL_TEXTURE_2D_ARRAY_EXT;
+            obj = &unit->Saved2DArray;
+            break;
+         default:
+            ; /* silence warnings */
+         }
+
+         _mesa_BindTexture(target, obj->Name);
+
+         bordColor[0] = CHAN_TO_FLOAT(obj->BorderColor[0]);
+         bordColor[1] = CHAN_TO_FLOAT(obj->BorderColor[1]);
+         bordColor[2] = CHAN_TO_FLOAT(obj->BorderColor[2]);
+         bordColor[3] = CHAN_TO_FLOAT(obj->BorderColor[3]);
+
+         _mesa_TexParameterf(target, GL_TEXTURE_PRIORITY, obj->Priority);
+         _mesa_TexParameterfv(target, GL_TEXTURE_BORDER_COLOR, bordColor);
+         _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, obj->WrapS);
+         _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, obj->WrapT);
+         _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, obj->WrapR);
+         _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, obj->MinFilter);
+         _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, obj->MagFilter);
+         _mesa_TexParameterf(target, GL_TEXTURE_MIN_LOD, obj->MinLod);
+         _mesa_TexParameterf(target, GL_TEXTURE_MAX_LOD, obj->MaxLod);
+         _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, obj->BaseLevel);
+         if (target != GL_TEXTURE_RECTANGLE_ARB)
+            _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, obj->MaxLevel);
+         if (ctx->Extensions.EXT_texture_filter_anisotropic) {
+            _mesa_TexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
+                                obj->MaxAnisotropy);
+         }
+         if (ctx->Extensions.SGIX_shadow) {
+            _mesa_TexParameteri(target, GL_TEXTURE_COMPARE_SGIX,
+                                obj->CompareFlag);
+            _mesa_TexParameteri(target, GL_TEXTURE_COMPARE_OPERATOR_SGIX,
+                                obj->CompareOperator);
+         }
+         if (ctx->Extensions.SGIX_shadow_ambient) {
+            _mesa_TexParameterf(target, GL_SHADOW_AMBIENT_SGIX,
+                                obj->ShadowAmbient);
+         }
+
+      }
+   }
+   _mesa_ActiveTextureARB(GL_TEXTURE0_ARB
+                          + texAttrib->CurrentUnit);
+
+   /* "un-bump" the texture object reference counts.  We did that so they
+    * wouldn't inadvertantly get deleted while they were still referenced
+    * inside the attribute state stack.
+    */
+   for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
+      ctx->Texture.Unit[u].Current1D->RefCount--;
+      ctx->Texture.Unit[u].Current2D->RefCount--;
+      ctx->Texture.Unit[u].Current3D->RefCount--;
+      ctx->Texture.Unit[u].CurrentCubeMap->RefCount--;
+      ctx->Texture.Unit[u].CurrentRect->RefCount--;
+   }
+}
+
 
 /*
  * This function is kind of long just because we have to call a lot
  * of device driver functions to update device driver state.
+ *
+ * XXX As it is now, most of the pop-code calls immediate-mode Mesa functions
+ * in order to restore GL state.  This isn't terribly efficient but it
+ * ensures that dirty flags and any derived state gets updated correctly.
+ * We could at least check if the value to restore equals the current value
+ * and then skip the Mesa call.
  */
-void
+void GLAPIENTRY
 _mesa_PopAttrib(void)
 {
    struct gl_attrib_node *attr, *next;
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   if (ctx->AttribStackDepth==0) {
-      gl_error( ctx, GL_STACK_UNDERFLOW, "glPopAttrib" );
+   if (ctx->AttribStackDepth == 0) {
+      _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopAttrib" );
       return;
    }
 
@@ -557,59 +844,93 @@ _mesa_PopAttrib(void)
 
    while (attr) {
 
-      if (MESA_VERBOSE&VERBOSE_API)
-        fprintf(stderr, "glPopAttrib %s\n", gl_lookup_enum_by_nr(attr->kind));
+      if (MESA_VERBOSE & VERBOSE_API) {
+         _mesa_debug(ctx, "glPopAttrib %s\n",
+                     _mesa_lookup_enum_by_nr(attr->kind));
+      }
 
       switch (attr->kind) {
          case GL_ACCUM_BUFFER_BIT:
-            MEMCPY( &ctx->Accum, attr->data, sizeof(struct gl_accum_attrib) );
-           ctx->NewState |= _NEW_ACCUM;
+            {
+               const struct gl_accum_attrib *accum;
+               accum = (const struct gl_accum_attrib *) attr->data;
+               _mesa_ClearAccum(accum->ClearColor[0],
+                                accum->ClearColor[1],
+                                accum->ClearColor[2],
+                                accum->ClearColor[3]);
+            }
             break;
          case GL_COLOR_BUFFER_BIT:
             {
-               GLenum oldDrawBuffer = ctx->Color.DrawBuffer;
-               GLenum oldAlphaFunc = ctx->Color.AlphaFunc;
-               GLchan oldAlphaRef = ctx->Color.AlphaRef;
-               GLenum oldBlendSrc = ctx->Color.BlendSrcRGB;
-               GLenum oldBlendDst = ctx->Color.BlendDstRGB;
-              GLenum oldLogicOp = ctx->Color.LogicOp;
-               MEMCPY( &ctx->Color, attr->data,
-                       sizeof(struct gl_colorbuffer_attrib) );
-              ctx->NewState |= _NEW_COLOR;
-               if (ctx->Color.DrawBuffer != oldDrawBuffer) {
-                  _mesa_DrawBuffer( ctx->Color.DrawBuffer);
-               }
-               if ((ctx->Color.BlendSrcRGB != oldBlendSrc ||
-                    ctx->Color.BlendDstRGB != oldBlendDst) &&
-                   ctx->Driver.BlendFunc)
-                  (*ctx->Driver.BlendFunc)( ctx, ctx->Color.BlendSrcRGB,
-                                            ctx->Color.BlendDstRGB);
-              if (ctx->Color.LogicOp != oldLogicOp &&
-                  ctx->Driver.LogicOpcode) {
-                 ctx->Driver.LogicOpcode( ctx, ctx->Color.LogicOp );
-               }
-               if (ctx->Visual.rgbMode) {
-                  GLchan r = (GLint) (ctx->Color.ClearColor[0] * CHAN_MAXF);
-                  GLchan g = (GLint) (ctx->Color.ClearColor[1] * CHAN_MAXF);
-                  GLchan b = (GLint) (ctx->Color.ClearColor[2] * CHAN_MAXF);
-                  GLchan a = (GLint) (ctx->Color.ClearColor[3] * CHAN_MAXF);
-                  (*ctx->Driver.ClearColor)( ctx, r, g, b, a );
-                  if ((ctx->Color.AlphaFunc != oldAlphaFunc ||
-                       ctx->Color.AlphaRef != oldAlphaRef) &&
-                      ctx->Driver.AlphaFunc)
-                     (*ctx->Driver.AlphaFunc)( ctx, ctx->Color.AlphaFunc,
-                                              ctx->Color.AlphaRef / CHAN_MAXF);
-                  if (ctx->Driver.ColorMask) {
-                     (*ctx->Driver.ColorMask)(ctx,
-                                              ctx->Color.ColorMask[0],
-                                              ctx->Color.ColorMask[1],
-                                              ctx->Color.ColorMask[2],
-                                              ctx->Color.ColorMask[3]);
+               const struct gl_colorbuffer_attrib *color;
+               color = (const struct gl_colorbuffer_attrib *) attr->data;
+               _mesa_ClearIndex((GLfloat) color->ClearIndex);
+               _mesa_ClearColor(color->ClearColor[0],
+                                color->ClearColor[1],
+                                color->ClearColor[2],
+                                color->ClearColor[3]);
+               _mesa_IndexMask(color->IndexMask);
+               _mesa_ColorMask((GLboolean) (color->ColorMask[0] != 0),
+                               (GLboolean) (color->ColorMask[1] != 0),
+                               (GLboolean) (color->ColorMask[2] != 0),
+                               (GLboolean) (color->ColorMask[3] != 0));
+               {
+                  /* Need to determine if more than one color output is
+                   * specified.  If so, call glDrawBuffersARB, else call
+                   * glDrawBuffer().  This is a subtle, but essential point
+                   * since GL_FRONT (for example) is illegal for the former
+                   * function, but legal for the later.
+                   */
+                  GLboolean multipleBuffers = GL_FALSE;
+                  if (ctx->Extensions.ARB_draw_buffers) {
+                     GLuint i;
+                     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
+                   * GL_DRAW_BUFFER.
+                   * Ex: if GL_FRONT were pushed, but we're popping with a
+                   * 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
+                     _mesa_DrawBuffer(color->DrawBuffer[0]);
                }
-               else {
-                  (*ctx->Driver.ClearIndex)( ctx, ctx->Color.ClearIndex);
-               }
+               _mesa_set_enable(ctx, GL_ALPHA_TEST, color->AlphaEnabled);
+               _mesa_AlphaFunc(color->AlphaFunc, color->AlphaRef);
+               _mesa_set_enable(ctx, GL_BLEND, color->BlendEnabled);
+               _mesa_BlendFuncSeparateEXT(color->BlendSrcRGB,
+                                          color->BlendDstRGB,
+                                          color->BlendSrcA,
+                                          color->BlendDstA);
+              /* This special case is because glBlendEquationSeparateEXT
+               * cannot take GL_LOGIC_OP as a parameter.
+               */
+              if ( color->BlendEquationRGB == color->BlendEquationA ) {
+                 _mesa_BlendEquation(color->BlendEquationRGB);
+              }
+              else {
+                 _mesa_BlendEquationSeparateEXT(color->BlendEquationRGB,
+                                                color->BlendEquationA);
+              }
+               _mesa_BlendColor(color->BlendColor[0],
+                                color->BlendColor[1],
+                                color->BlendColor[2],
+                                color->BlendColor[3]);
+               _mesa_LogicOp(color->LogicOp);
+               _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP,
+                                color->ColorLogicOpEnabled);
+               _mesa_set_enable(ctx, GL_INDEX_LOGIC_OP,
+                                color->IndexLogicOpEnabled);
+               _mesa_set_enable(ctx, GL_DITHER, color->DitherFlag);
             }
             break;
          case GL_CURRENT_BIT:
@@ -619,21 +940,12 @@ _mesa_PopAttrib(void)
             break;
          case GL_DEPTH_BUFFER_BIT:
             {
-               GLboolean oldDepthTest = ctx->Depth.Test;
-               GLenum oldDepthFunc = ctx->Depth.Func;
-               GLboolean oldDepthMask = ctx->Depth.Mask;
-               GLfloat oldDepthClear = ctx->Depth.Clear;
-               MEMCPY( &ctx->Depth, attr->data,
-                       sizeof(struct gl_depthbuffer_attrib) );
-              ctx->NewState |= _NEW_DEPTH;
-               if (ctx->Depth.Test != oldDepthTest && ctx->Driver.Enable)
-                  (*ctx->Driver.Enable)( ctx, GL_DEPTH_TEST, ctx->Depth.Test);
-               if (ctx->Depth.Func != oldDepthFunc && ctx->Driver.DepthFunc)
-                  (*ctx->Driver.DepthFunc)( ctx, ctx->Depth.Func );
-               if (ctx->Depth.Mask != oldDepthMask && ctx->Driver.DepthMask)
-                  (*ctx->Driver.DepthMask)( ctx, ctx->Depth.Mask );
-               if (ctx->Depth.Clear != oldDepthClear && ctx->Driver.ClearDepth)
-                  (*ctx->Driver.ClearDepth)( ctx, ctx->Depth.Clear );
+               const struct gl_depthbuffer_attrib *depth;
+               depth = (const struct gl_depthbuffer_attrib *) attr->data;
+               _mesa_DepthFunc(depth->Func);
+               _mesa_ClearDepth(depth->Clear);
+               _mesa_set_enable(ctx, GL_DEPTH_TEST, depth->Test);
+               _mesa_DepthMask(depth->Mask);
             }
             break;
          case GL_ENABLE_BIT:
@@ -650,109 +962,152 @@ _mesa_PopAttrib(void)
             break;
          case GL_FOG_BIT:
             {
-               GLboolean anyChange = (GLboolean) (memcmp( &ctx->Fog, attr->data, sizeof(struct gl_fog_attrib) ) != 0);
-               MEMCPY( &ctx->Fog, attr->data, sizeof(struct gl_fog_attrib) );
-              ctx->NewState |= _NEW_FOG;
-               if (anyChange && ctx->Driver.Fogfv) {
-                  const GLfloat mode = (GLfloat) ctx->Fog.Mode;
-                  const GLfloat density = ctx->Fog.Density;
-                  const GLfloat start = ctx->Fog.Start;
-                  const GLfloat end = ctx->Fog.End;
-                  const GLfloat index = ctx->Fog.Index;
-                  (*ctx->Driver.Fogfv)( ctx, GL_FOG_MODE, &mode);
-                  (*ctx->Driver.Fogfv)( ctx, GL_FOG_DENSITY, &density );
-                  (*ctx->Driver.Fogfv)( ctx, GL_FOG_START, &start );
-                  (*ctx->Driver.Fogfv)( ctx, GL_FOG_END, &end );
-                  (*ctx->Driver.Fogfv)( ctx, GL_FOG_INDEX, &index );
-                  (*ctx->Driver.Fogfv)( ctx, GL_FOG_COLOR, ctx->Fog.Color );
-               }
-              ctx->_Enabled &= ~ENABLE_FOG;
-              if (ctx->Fog.Enabled) ctx->_Enabled |= ENABLE_FOG;
+               const struct gl_fog_attrib *fog;
+               fog = (const struct gl_fog_attrib *) attr->data;
+               _mesa_set_enable(ctx, GL_FOG, fog->Enabled);
+               _mesa_Fogfv(GL_FOG_COLOR, fog->Color);
+               _mesa_Fogf(GL_FOG_DENSITY, fog->Density);
+               _mesa_Fogf(GL_FOG_START, fog->Start);
+               _mesa_Fogf(GL_FOG_END, fog->End);
+               _mesa_Fogf(GL_FOG_INDEX, fog->Index);
+               _mesa_Fogi(GL_FOG_MODE, fog->Mode);
             }
             break;
          case GL_HINT_BIT:
-            MEMCPY( &ctx->Hint, attr->data, sizeof(struct gl_hint_attrib) );
-           ctx->NewState |= _NEW_HINT;
-            if (ctx->Driver.Hint) {
-               (*ctx->Driver.Hint)( ctx, GL_PERSPECTIVE_CORRECTION_HINT,
-                                    ctx->Hint.PerspectiveCorrection );
-               (*ctx->Driver.Hint)( ctx, GL_POINT_SMOOTH_HINT,
-                                    ctx->Hint.PointSmooth);
-               (*ctx->Driver.Hint)( ctx, GL_LINE_SMOOTH_HINT,
-                                    ctx->Hint.LineSmooth );
-               (*ctx->Driver.Hint)( ctx, GL_POLYGON_SMOOTH_HINT,
-                                    ctx->Hint.PolygonSmooth );
-               (*ctx->Driver.Hint)( ctx, GL_FOG_HINT, ctx->Hint.Fog );
+            {
+               const struct gl_hint_attrib *hint;
+               hint = (const struct gl_hint_attrib *) attr->data;
+               _mesa_Hint(GL_PERSPECTIVE_CORRECTION_HINT,
+                          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);
+               if (ctx->Extensions.ARB_texture_compression)
+                  _mesa_Hint(GL_TEXTURE_COMPRESSION_HINT_ARB,
+                             hint->TextureCompression);
             }
             break;
          case GL_LIGHTING_BIT:
-            MEMCPY( &ctx->Light, attr->data, sizeof(struct gl_light_attrib) );
-           ctx->NewState |= _NEW_LIGHT;
-            if (ctx->Driver.Enable) {
+            {
                GLuint i;
-               for (i = 0; i < MAX_LIGHTS; i++) {
-                  GLenum light = (GLenum) (GL_LIGHT0 + i);
-                  (*ctx->Driver.Enable)( ctx, light, ctx->Light.Light[i].Enabled );
+               const struct gl_light_attrib *light;
+               light = (const struct gl_light_attrib *) attr->data;
+               /* lighting enable */
+               _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 );
+              
+               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_POSITION, l->EyePosition);
+                 _mesa_light(ctx, i, GL_SPOT_DIRECTION, l->EyeDirection);
+                 _mesa_light(ctx, i, GL_SPOT_EXPONENT, &l->SpotExponent);
+                 _mesa_light(ctx, i, GL_SPOT_CUTOFF, &l->SpotCutoff);
+                 _mesa_light(ctx, i, GL_CONSTANT_ATTENUATION,
+                              &l->ConstantAttenuation);
+                 _mesa_light(ctx, i, GL_LINEAR_ATTENUATION,
+                              &l->LinearAttenuation);
+                 _mesa_light(ctx, i, GL_QUADRATIC_ATTENUATION,
+                              &l->QuadraticAttenuation);
                }
-               (*ctx->Driver.Enable)( ctx, GL_LIGHTING, ctx->Light.Enabled );
+               /* light model */
+               _mesa_LightModelfv(GL_LIGHT_MODEL_AMBIENT,
+                                  light->Model.Ambient);
+               _mesa_LightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,
+                                 (GLfloat) light->Model.LocalViewer);
+               _mesa_LightModelf(GL_LIGHT_MODEL_TWO_SIDE,
+                                 (GLfloat) light->Model.TwoSide);
+               _mesa_LightModelf(GL_LIGHT_MODEL_COLOR_CONTROL,
+                                 (GLfloat) light->Model.ColorControl);
+               /* shade model */
+               _mesa_ShadeModel(light->ShadeModel);
+               /* color material */
+               _mesa_ColorMaterial(light->ColorMaterialFace,
+                                   light->ColorMaterialMode);
+               _mesa_set_enable(ctx, GL_COLOR_MATERIAL,
+                                light->ColorMaterialEnabled);
+               /* materials */
+               MEMCPY(&ctx->Light.Material, &light->Material,
+                      sizeof(struct gl_material));
             }
-            if (ctx->Light.ShadeModel == GL_FLAT)
-               ctx->_TriangleCaps |= DD_FLATSHADE;
-            else
-               ctx->_TriangleCaps &= ~DD_FLATSHADE;
-            if (ctx->Driver.ShadeModel)
-               (*ctx->Driver.ShadeModel)(ctx, ctx->Light.ShadeModel);
-           ctx->_Enabled &= ~ENABLE_LIGHT;
-           if (ctx->Light.Enabled)
-              ctx->_Enabled |= ENABLE_LIGHT;
             break;
          case GL_LINE_BIT:
-            MEMCPY( &ctx->Line, attr->data, sizeof(struct gl_line_attrib) );
-           ctx->NewState |= _NEW_LINE;
-            if (ctx->Driver.Enable) {
-               (*ctx->Driver.Enable)( ctx, GL_LINE_SMOOTH, ctx->Line.SmoothFlag );
-               (*ctx->Driver.Enable)( ctx, GL_LINE_STIPPLE, ctx->Line.StippleFlag );
+            {
+               const struct gl_line_attrib *line;
+               line = (const struct gl_line_attrib *) attr->data;
+               _mesa_set_enable(ctx, GL_LINE_SMOOTH, line->SmoothFlag);
+               _mesa_set_enable(ctx, GL_LINE_STIPPLE, line->StippleFlag);
+               _mesa_LineStipple(line->StippleFactor, line->StipplePattern);
+               _mesa_LineWidth(line->Width);
             }
-            if (ctx->Driver.LineStipple)
-               (*ctx->Driver.LineStipple)(ctx, ctx->Line.StippleFactor,
-                                          ctx->Line.StipplePattern);
-            if (ctx->Driver.LineWidth)
-               (*ctx->Driver.LineWidth)(ctx, ctx->Line.Width);
             break;
          case GL_LIST_BIT:
             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) );
+            /* XXX what other pixel state needs to be set by function calls? */
+            _mesa_ReadBuffer(ctx->Pixel.ReadBuffer);
            ctx->NewState |= _NEW_PIXEL;
             break;
          case GL_POINT_BIT:
-            MEMCPY( &ctx->Point, attr->data, sizeof(struct gl_point_attrib) );
-           ctx->NewState |= _NEW_POINT;
-            if (ctx->Driver.Enable)
-               (*ctx->Driver.Enable)( ctx, GL_POINT_SMOOTH, ctx->Point.SmoothFlag );
+            {
+               const struct gl_point_attrib *point;
+               point = (const struct gl_point_attrib *) attr->data;
+               _mesa_PointSize(point->Size);
+               _mesa_set_enable(ctx, GL_POINT_SMOOTH, point->SmoothFlag);
+               if (ctx->Extensions.EXT_point_parameters) {
+                  _mesa_PointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT,
+                                            point->Params);
+                  _mesa_PointParameterfEXT(GL_POINT_SIZE_MIN_EXT,
+                                           point->MinSize);
+                  _mesa_PointParameterfEXT(GL_POINT_SIZE_MAX_EXT,
+                                           point->MaxSize);
+                  _mesa_PointParameterfEXT(GL_POINT_FADE_THRESHOLD_SIZE_EXT,
+                                           point->Threshold);
+               }
+               if (ctx->Extensions.NV_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]);
+                  }
+                  _mesa_set_enable(ctx, GL_POINT_SPRITE_NV,point->PointSprite);
+                  _mesa_PointParameteriNV(GL_POINT_SPRITE_R_MODE_NV,
+                                          ctx->Point.SpriteRMode);
+                  _mesa_PointParameterfEXT(GL_POINT_SPRITE_COORD_ORIGIN,
+                                           (GLfloat)ctx->Point.SpriteOrigin);
+               }
+            }
             break;
          case GL_POLYGON_BIT:
             {
-               GLenum oldFrontMode = ctx->Polygon.FrontMode;
-               GLenum oldBackMode = ctx->Polygon.BackMode;
-               MEMCPY( &ctx->Polygon, attr->data,
-                       sizeof(struct gl_polygon_attrib) );
-              ctx->NewState |= _NEW_POLYGON;
-               if ((ctx->Polygon.FrontMode != oldFrontMode ||
-                    ctx->Polygon.BackMode != oldBackMode) &&
-                   ctx->Driver.PolygonMode) {
-                  (*ctx->Driver.PolygonMode)( ctx, GL_FRONT, ctx->Polygon.FrontMode);
-                  (*ctx->Driver.PolygonMode)( ctx, GL_BACK, ctx->Polygon.BackMode);
-               }
-              if (ctx->Driver.CullFace)
-                 ctx->Driver.CullFace( ctx, ctx->Polygon.CullFaceMode );
-
-              if (ctx->Driver.FrontFace)
-                 ctx->Driver.FrontFace( ctx, ctx->Polygon.FrontFace );
-
-               if (ctx->Driver.Enable)
-                  (*ctx->Driver.Enable)( ctx, GL_POLYGON_SMOOTH, ctx->Polygon.SmoothFlag );
+               const struct gl_polygon_attrib *polygon;
+               polygon = (const struct gl_polygon_attrib *) attr->data;
+               _mesa_CullFace(polygon->CullFaceMode);
+               _mesa_FrontFace(polygon->FrontFace);
+               _mesa_PolygonMode(GL_FRONT, polygon->FrontMode);
+               _mesa_PolygonMode(GL_BACK, polygon->BackMode);
+               _mesa_PolygonOffset(polygon->OffsetFactor,
+                                   polygon->OffsetUnits);
+               _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);
+               _mesa_set_enable(ctx, GL_POLYGON_OFFSET_POINT,
+                                polygon->OffsetPoint);
+               _mesa_set_enable(ctx, GL_POLYGON_OFFSET_LINE,
+                                polygon->OffsetLine);
+               _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL,
+                                polygon->OffsetFill);
             }
             break;
         case GL_POLYGON_STIPPLE_BIT:
@@ -762,91 +1117,106 @@ _mesa_PopAttrib(void)
               ctx->Driver.PolygonStipple( ctx, (const GLubyte *) attr->data );
            break;
          case GL_SCISSOR_BIT:
-            MEMCPY( &ctx->Scissor, attr->data,
-                   sizeof(struct gl_scissor_attrib) );
-           ctx->NewState |= _NEW_SCISSOR;
-            if (ctx->Driver.Enable)
-               (*ctx->Driver.Enable)( ctx, GL_SCISSOR_TEST, ctx->Scissor.Enabled );
-           if (ctx->Driver.Scissor)
-              ctx->Driver.Scissor( ctx, ctx->Scissor.X, ctx->Scissor.Y,
-                                   ctx->Scissor.Width, ctx->Scissor.Height );
+            {
+               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);
+            }
             break;
          case GL_STENCIL_BUFFER_BIT:
-            MEMCPY( &ctx->Stencil, attr->data,
-                   sizeof(struct gl_stencil_attrib) );
-           ctx->NewState |= _NEW_STENCIL;
-            if (ctx->Driver.StencilFunc)
-               (*ctx->Driver.StencilFunc)( ctx, ctx->Stencil.Function,
-                                   ctx->Stencil.Ref, ctx->Stencil.ValueMask);
-            if (ctx->Driver.StencilMask)
-               (*ctx->Driver.StencilMask)( ctx, ctx->Stencil.WriteMask );
-            if (ctx->Driver.StencilOp)
-               (*ctx->Driver.StencilOp)( ctx, ctx->Stencil.FailFunc,
-                              ctx->Stencil.ZFailFunc, ctx->Stencil.ZPassFunc);
-            if (ctx->Driver.ClearStencil)
-               (*ctx->Driver.ClearStencil)( ctx, ctx->Stencil.Clear );
-            if (ctx->Driver.Enable)
-               (*ctx->Driver.Enable)( ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled );
-           ctx->_TriangleCaps &= ~DD_STENCIL;
-           if (ctx->Stencil.Enabled)
-              ctx->_TriangleCaps |= DD_STENCIL;
-
+            {
+               const struct gl_stencil_attrib *stencil;
+               stencil = (const struct gl_stencil_attrib *) attr->data;
+               _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
+               _mesa_ClearStencil(stencil->Clear);
+               if (ctx->Extensions.EXT_stencil_two_side) {
+                  _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
+                                   stencil->TestTwoSide);
+                  _mesa_ActiveStencilFaceEXT(stencil->ActiveFace
+                                             ? GL_BACK : GL_FRONT);
+               }
+               /* front state */
+               _mesa_StencilFuncSeparate(GL_FRONT,
+                                         stencil->Function[0],
+                                         stencil->Ref[0],
+                                         stencil->ValueMask[0]);
+               _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
+               _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
+                                       stencil->ZFailFunc[0],
+                                       stencil->ZPassFunc[0]);
+               /* back state */
+               _mesa_StencilFuncSeparate(GL_BACK,
+                                         stencil->Function[1],
+                                         stencil->Ref[1],
+                                         stencil->ValueMask[1]);
+               _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
+               _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
+                                       stencil->ZFailFunc[1],
+                                       stencil->ZPassFunc[1]);
+            }
             break;
          case GL_TRANSFORM_BIT:
-            MEMCPY( &ctx->Transform, attr->data,
-                   sizeof(struct gl_transform_attrib) );
-           ctx->NewState |= _NEW_TRANSFORM;
-            if (ctx->Driver.Enable) {
-               (*ctx->Driver.Enable)( ctx, GL_NORMALIZE, ctx->Transform.Normalize );
-               (*ctx->Driver.Enable)( ctx, GL_RESCALE_NORMAL_EXT, ctx->Transform.RescaleNormals );
+            {
+               GLuint i;
+               const struct gl_transform_attrib *xform;
+               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 );
+
+               /* restore clip planes */
+               for (i = 0; i < MAX_CLIP_PLANES; i++) {
+                  const GLuint mask = 1 << 1;
+                  const GLfloat *eyePlane = xform->EyeUserPlane[i];
+                  COPY_4V(ctx->Transform.EyeUserPlane[i], eyePlane);
+                  if (xform->ClipPlanesEnabled & mask) {
+                     _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
+                  }
+                  else {
+                     _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
+                  }
+                  if (ctx->Driver.ClipPlane)
+                     ctx->Driver.ClipPlane( ctx, GL_CLIP_PLANE0 + i, eyePlane );
+               }
+
+               /* normalize/rescale */
+               if (xform->Normalize != ctx->Transform.Normalize)
+                  _mesa_set_enable(ctx, GL_NORMALIZE,ctx->Transform.Normalize);
+               if (xform->RescaleNormals != ctx->Transform.RescaleNormals)
+                  _mesa_set_enable(ctx, GL_RESCALE_NORMAL_EXT,
+                                   ctx->Transform.RescaleNormals);
             }
-           ctx->_Enabled &= ~(ENABLE_NORMALIZE|ENABLE_RESCALE);
-           if (ctx->Transform.Normalize) ctx->_Enabled |= ENABLE_NORMALIZE;
-           if (ctx->Transform.RescaleNormals) ctx->_Enabled |= ENABLE_RESCALE;
             break;
          case GL_TEXTURE_BIT:
             /* Take care of texture object reference counters */
             {
-               GLuint u;
-               for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
-                 ctx->Texture.Unit[u].Current1D->RefCount--;
-                 ctx->Texture.Unit[u].Current2D->RefCount--;
-                 ctx->Texture.Unit[u].Current3D->RefCount--;
-                 ctx->Texture.Unit[u].CurrentCubeMap->RefCount--;
-               }
-               MEMCPY( &ctx->Texture, attr->data,
-                       sizeof(struct gl_texture_attrib) );
+               const struct gl_texture_attrib *texture;
+               texture = (const struct gl_texture_attrib *) attr->data;
+               pop_texture_group(ctx, texture);
               ctx->NewState |= _NEW_TEXTURE;
-               /* restore state of the currently bound texture objects */
-               for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
-                  copy_texobj_state( ctx->Texture.Unit[u].Current1D,
-                                     &(ctx->Texture.Unit[u].Saved1D) );
-                  copy_texobj_state( ctx->Texture.Unit[u].Current2D,
-                                     &(ctx->Texture.Unit[u].Saved2D) );
-                  copy_texobj_state( ctx->Texture.Unit[u].Current3D,
-                                     &(ctx->Texture.Unit[u].Saved3D) );
-                  copy_texobj_state( ctx->Texture.Unit[u].CurrentCubeMap,
-                                     &(ctx->Texture.Unit[u].SavedCubeMap) );
-
-                  ctx->Texture.Unit[u].Current1D->Complete = GL_FALSE;
-                  ctx->Texture.Unit[u].Current2D->Complete = GL_FALSE;
-                  ctx->Texture.Unit[u].Current3D->Complete = GL_FALSE;
-                  ctx->Texture.Unit[u].CurrentCubeMap->Complete = GL_FALSE;
-               }
             }
             break;
          case GL_VIEWPORT_BIT:
-        {
-           struct gl_viewport_attrib *v =
-              (struct gl_viewport_attrib *)attr->data;
+            {
+               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);
+            }
+            break;
+         case GL_MULTISAMPLE_BIT_ARB:
+            {
+               const struct gl_multisample_attrib *ms;
+               ms = (const struct gl_multisample_attrib *) attr->data;
+               _mesa_SampleCoverageARB(ms->SampleCoverageValue,
+                                       ms->SampleCoverageInvert);
+            }
+            break;
 
-           ctx->NewState |= _NEW_VIEWPORT;
-           _mesa_Viewport( v->X, v->Y, v->Width, v->Height );
-           _mesa_DepthRange( v->Near, v->Far );
-           break;
-        }
          default:
-            gl_problem( ctx, "Bad attrib flag in PopAttrib");
+            _mesa_problem( ctx, "Bad attrib flag in PopAttrib");
             break;
       }
 
@@ -858,11 +1228,36 @@ _mesa_PopAttrib(void)
 }
 
 
+/**
+ * Helper for incrementing/decrementing vertex buffer object reference
+ * counts when pushing/popping the GL_CLIENT_VERTEX_ARRAY_BIT attribute group.
+ */
+static void
+adjust_buffer_object_ref_counts(struct gl_array_attrib *array, GLint step)
+{
+   GLuint i;
+   array->ArrayObj->Vertex.BufferObj->RefCount += step;
+   array->ArrayObj->Normal.BufferObj->RefCount += step;
+   array->ArrayObj->Color.BufferObj->RefCount += step;
+   array->ArrayObj->SecondaryColor.BufferObj->RefCount += step;
+   array->ArrayObj->FogCoord.BufferObj->RefCount += step;
+   array->ArrayObj->Index.BufferObj->RefCount += step;
+   array->ArrayObj->EdgeFlag.BufferObj->RefCount += step;
+   for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++)
+      array->ArrayObj->TexCoord[i].BufferObj->RefCount += step;
+   for (i = 0; i < VERT_ATTRIB_MAX; i++)
+      array->ArrayObj->VertexAttrib[i].BufferObj->RefCount += step;
+
+   array->ArrayBufferObj->RefCount += step;
+   array->ElementArrayBufferObj->RefCount += step;
+}
+
+
 #define GL_CLIENT_PACK_BIT (1<<20)
 #define GL_CLIENT_UNPACK_BIT (1<<21)
 
 
-void
+void GLAPIENTRY
 _mesa_PushClientAttrib(GLbitfield mask)
 {
    struct gl_attrib_node *newnode;
@@ -871,8 +1266,8 @@ _mesa_PushClientAttrib(GLbitfield mask)
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   if (ctx->ClientAttribStackDepth>=MAX_CLIENT_ATTRIB_STACK_DEPTH) {
-      gl_error( ctx, GL_STACK_OVERFLOW, "glPushClientAttrib" );
+   if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
+      _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushClientAttrib" );
       return;
    }
 
@@ -882,6 +1277,10 @@ _mesa_PushClientAttrib(GLbitfield mask)
 
    if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
       struct gl_pixelstore_attrib *attr;
+#if FEATURE_EXT_pixel_buffer_object
+      ctx->Pack.BufferObj->RefCount++;
+      ctx->Unpack.BufferObj->RefCount++;
+#endif
       /* packing attribs */
       attr = MALLOC_STRUCT( gl_pixelstore_attrib );
       MEMCPY( attr, &ctx->Pack, sizeof(struct gl_pixelstore_attrib) );
@@ -899,12 +1298,28 @@ _mesa_PushClientAttrib(GLbitfield mask)
    }
    if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
       struct gl_array_attrib *attr;
+      struct gl_array_object *obj;
+
       attr = MALLOC_STRUCT( gl_array_attrib );
+      obj = MALLOC_STRUCT( gl_array_object );
+
+#if FEATURE_ARB_vertex_buffer_object
+      /* increment ref counts since we're copying pointers to these objects */
+      ctx->Array.ArrayBufferObj->RefCount++;
+      ctx->Array.ElementArrayBufferObj->RefCount++;
+#endif
+
       MEMCPY( attr, &ctx->Array, sizeof(struct gl_array_attrib) );
+      MEMCPY( obj, ctx->Array.ArrayObj, sizeof(struct gl_array_object) );
+
+      attr->ArrayObj = obj;
+
       newnode = new_attrib_node( GL_CLIENT_VERTEX_ARRAY_BIT );
       newnode->data = attr;
       newnode->next = head;
       head = newnode;
+      /* bump reference counts on buffer objects */
+      adjust_buffer_object_ref_counts(&ctx->Array, 1);
    }
 
    ctx->ClientAttribStack[ctx->ClientAttribStackDepth] = head;
@@ -914,7 +1329,7 @@ _mesa_PushClientAttrib(GLbitfield mask)
 
 
 
-void
+void GLAPIENTRY
 _mesa_PopClientAttrib(void)
 {
    struct gl_attrib_node *attr, *next;
@@ -922,8 +1337,8 @@ _mesa_PopClientAttrib(void)
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   if (ctx->ClientAttribStackDepth==0) {
-      gl_error( ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib" );
+   if (ctx->ClientAttribStackDepth == 0) {
+      _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib" );
       return;
    }
 
@@ -933,22 +1348,63 @@ _mesa_PopClientAttrib(void)
    while (attr) {
       switch (attr->kind) {
          case GL_CLIENT_PACK_BIT:
+#if FEATURE_EXT_pixel_buffer_object
+            ctx->Pack.BufferObj->RefCount--;
+            if (ctx->Pack.BufferObj->RefCount <= 0) {
+               _mesa_remove_buffer_object( ctx, ctx->Pack.BufferObj );
+               (*ctx->Driver.DeleteBuffer)( ctx, ctx->Pack.BufferObj );
+            }
+#endif
             MEMCPY( &ctx->Pack, attr->data,
                     sizeof(struct gl_pixelstore_attrib) );
-           ctx->NewState = _NEW_PACKUNPACK;
+           ctx->NewState |= _NEW_PACKUNPACK;
             break;
          case GL_CLIENT_UNPACK_BIT:
+#if FEATURE_EXT_pixel_buffer_object
+            ctx->Unpack.BufferObj->RefCount--;
+            if (ctx->Unpack.BufferObj->RefCount <= 0) {
+               _mesa_remove_buffer_object( ctx, ctx->Unpack.BufferObj );
+               (*ctx->Driver.DeleteBuffer)( ctx, ctx->Unpack.BufferObj );
+            }
+#endif
             MEMCPY( &ctx->Unpack, attr->data,
                     sizeof(struct gl_pixelstore_attrib) );
-           ctx->NewState = _NEW_PACKUNPACK;
+           ctx->NewState |= _NEW_PACKUNPACK;
             break;
-         case GL_CLIENT_VERTEX_ARRAY_BIT:
-            MEMCPY( &ctx->Array, attr->data,
-                   sizeof(struct gl_array_attrib) );
-           ctx->NewState = _NEW_ARRAY;
+         case GL_CLIENT_VERTEX_ARRAY_BIT: {
+           struct gl_array_attrib * data =
+             (struct gl_array_attrib *) attr->data;
+
+            adjust_buffer_object_ref_counts(&ctx->Array, -1);
+        
+            ctx->Array.ActiveTexture = data->ActiveTexture;
+           ctx->Array.LockFirst = data->LockFirst;
+           ctx->Array.LockCount = data->LockCount;
+
+           _mesa_BindVertexArrayAPPLE( data->ArrayObj->Name );
+           
+#if FEATURE_ARB_vertex_buffer_object
+            _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB,
+                                data->ArrayBufferObj->Name);
+            _mesa_BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
+                                data->ElementArrayBufferObj->Name);
+#endif
+
+           MEMCPY( ctx->Array.ArrayObj, data->ArrayObj,
+                   sizeof( struct gl_array_object ) );
+
+           FREE( data->ArrayObj );
+           
+           /* FIXME: Should some bits in ctx->Array->NewState also be set
+            * FIXME: here?  It seems like it should be set to inclusive-or
+            * FIXME: of the old ArrayObj->_Enabled and the new _Enabled.
+            */
+
+           ctx->NewState |= _NEW_ARRAY;
             break;
+        }
          default:
-            gl_problem( ctx, "Bad attrib flag in PopClientAttrib");
+            _mesa_problem( ctx, "Bad attrib flag in PopClientAttrib");
             break;
       }
 
@@ -957,8 +1413,12 @@ _mesa_PopClientAttrib(void)
       FREE( attr );
       attr = next;
    }
-
 }
 
 
-
+void _mesa_init_attrib( GLcontext *ctx )
+{
+   /* Renderer and client attribute stacks */
+   ctx->AttribStackDepth = 0;
+   ctx->ClientAttribStackDepth = 0;
+}