attrib.c revision 6c227e57e69158e4da40c69322db0eac4c31086c
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.6
4 *
5 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
6 * Copyright (C) 2009  VMware, Inc.   All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#include "glheader.h"
27#include "imports.h"
28#include "accum.h"
29#include "arrayobj.h"
30#include "attrib.h"
31#include "blend.h"
32#include "buffers.h"
33#include "bufferobj.h"
34#include "clear.h"
35#include "colormac.h"
36#include "context.h"
37#include "depth.h"
38#include "enable.h"
39#include "enums.h"
40#include "fog.h"
41#include "hint.h"
42#include "light.h"
43#include "lines.h"
44#include "macros.h"
45#include "matrix.h"
46#include "multisample.h"
47#include "points.h"
48#include "polygon.h"
49#include "scissor.h"
50#include "stencil.h"
51#include "texenv.h"
52#include "texgen.h"
53#include "texobj.h"
54#include "texparam.h"
55#include "texstate.h"
56#include "varray.h"
57#include "viewport.h"
58#include "mtypes.h"
59#include "main/dispatch.h"
60
61
62/**
63 * glEnable()/glDisable() attribute group (GL_ENABLE_BIT).
64 */
65struct gl_enable_attrib
66{
67   GLboolean AlphaTest;
68   GLboolean AutoNormal;
69   GLboolean Blend;
70   GLbitfield ClipPlanes;
71   GLboolean ColorMaterial;
72   GLboolean Convolution1D;
73   GLboolean Convolution2D;
74   GLboolean Separable2D;
75   GLboolean CullFace;
76   GLboolean DepthClamp;
77   GLboolean DepthTest;
78   GLboolean Dither;
79   GLboolean Fog;
80   GLboolean Light[MAX_LIGHTS];
81   GLboolean Lighting;
82   GLboolean LineSmooth;
83   GLboolean LineStipple;
84   GLboolean IndexLogicOp;
85   GLboolean ColorLogicOp;
86
87   GLboolean Map1Color4;
88   GLboolean Map1Index;
89   GLboolean Map1Normal;
90   GLboolean Map1TextureCoord1;
91   GLboolean Map1TextureCoord2;
92   GLboolean Map1TextureCoord3;
93   GLboolean Map1TextureCoord4;
94   GLboolean Map1Vertex3;
95   GLboolean Map1Vertex4;
96   GLboolean Map1Attrib[16];  /* GL_NV_vertex_program */
97   GLboolean Map2Color4;
98   GLboolean Map2Index;
99   GLboolean Map2Normal;
100   GLboolean Map2TextureCoord1;
101   GLboolean Map2TextureCoord2;
102   GLboolean Map2TextureCoord3;
103   GLboolean Map2TextureCoord4;
104   GLboolean Map2Vertex3;
105   GLboolean Map2Vertex4;
106   GLboolean Map2Attrib[16];  /* GL_NV_vertex_program */
107
108   GLboolean Normalize;
109   GLboolean PixelTexture;
110   GLboolean PointSmooth;
111   GLboolean PolygonOffsetPoint;
112   GLboolean PolygonOffsetLine;
113   GLboolean PolygonOffsetFill;
114   GLboolean PolygonSmooth;
115   GLboolean PolygonStipple;
116   GLboolean RescaleNormals;
117   GLboolean Scissor;
118   GLboolean Stencil;
119   GLboolean StencilTwoSide;          /* GL_EXT_stencil_two_side */
120   GLboolean MultisampleEnabled;      /* GL_ARB_multisample */
121   GLboolean SampleAlphaToCoverage;   /* GL_ARB_multisample */
122   GLboolean SampleAlphaToOne;        /* GL_ARB_multisample */
123   GLboolean SampleCoverage;          /* GL_ARB_multisample */
124   GLboolean SampleCoverageInvert;    /* GL_ARB_multisample */
125   GLboolean RasterPositionUnclipped; /* GL_IBM_rasterpos_clip */
126
127   GLbitfield Texture[MAX_TEXTURE_UNITS];
128   GLbitfield TexGen[MAX_TEXTURE_UNITS];
129
130   /* SGI_texture_color_table */
131   GLboolean TextureColorTable[MAX_TEXTURE_UNITS];
132
133   /* GL_ARB_vertex_program / GL_NV_vertex_program */
134   GLboolean VertexProgram;
135   GLboolean VertexProgramPointSize;
136   GLboolean VertexProgramTwoSide;
137
138   /* GL_ARB_point_sprite / GL_NV_point_sprite */
139   GLboolean PointSprite;
140   GLboolean FragmentShaderATI;
141};
142
143
144/**
145 * Node for the attribute stack.
146 */
147struct gl_attrib_node
148{
149   GLbitfield kind;
150   void *data;
151   struct gl_attrib_node *next;
152};
153
154
155
156/**
157 * Special struct for saving/restoring texture state (GL_TEXTURE_BIT)
158 */
159struct texture_state
160{
161   struct gl_texture_attrib Texture;  /**< The usual context state */
162
163   /** to save per texture object state (wrap modes, filters, etc): */
164   struct gl_texture_object SavedObj[MAX_TEXTURE_UNITS][NUM_TEXTURE_TARGETS];
165
166   /**
167    * To save references to texture objects (so they don't get accidentally
168    * deleted while saved in the attribute stack).
169    */
170   struct gl_texture_object *SavedTexRef[MAX_TEXTURE_UNITS][NUM_TEXTURE_TARGETS];
171};
172
173
174#if FEATURE_attrib_stack
175
176
177/**
178 * Allocate new attribute node of given type/kind.  Attach payload data.
179 * Insert it into the linked list named by 'head'.
180 */
181static void
182save_attrib_data(struct gl_attrib_node **head,
183                 GLbitfield kind, void *payload)
184{
185   struct gl_attrib_node *n = MALLOC_STRUCT(gl_attrib_node);
186   if (n) {
187      n->kind = kind;
188      n->data = payload;
189      /* insert at head */
190      n->next = *head;
191      *head = n;
192   }
193   else {
194      /* out of memory! */
195   }
196}
197
198
199void GLAPIENTRY
200_mesa_PushAttrib(GLbitfield mask)
201{
202   struct gl_attrib_node *head;
203
204   GET_CURRENT_CONTEXT(ctx);
205   ASSERT_OUTSIDE_BEGIN_END(ctx);
206
207   if (MESA_VERBOSE & VERBOSE_API)
208      _mesa_debug(ctx, "glPushAttrib %x\n", (int) mask);
209
210   if (ctx->AttribStackDepth >= MAX_ATTRIB_STACK_DEPTH) {
211      _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushAttrib" );
212      return;
213   }
214
215   /* Build linked list of attribute nodes which save all attribute */
216   /* groups specified by the mask. */
217   head = NULL;
218
219   if (mask & GL_ACCUM_BUFFER_BIT) {
220      struct gl_accum_attrib *attr;
221      attr = MALLOC_STRUCT( gl_accum_attrib );
222      memcpy( attr, &ctx->Accum, sizeof(struct gl_accum_attrib) );
223      save_attrib_data(&head, GL_ACCUM_BUFFER_BIT, attr);
224   }
225
226   if (mask & GL_COLOR_BUFFER_BIT) {
227      GLuint i;
228      struct gl_colorbuffer_attrib *attr;
229      attr = MALLOC_STRUCT( gl_colorbuffer_attrib );
230      memcpy( attr, &ctx->Color, sizeof(struct gl_colorbuffer_attrib) );
231      /* push the Draw FBO's DrawBuffer[] state, not ctx->Color.DrawBuffer[] */
232      for (i = 0; i < ctx->Const.MaxDrawBuffers; i ++)
233         attr->DrawBuffer[i] = ctx->DrawBuffer->ColorDrawBuffer[i];
234      save_attrib_data(&head, GL_COLOR_BUFFER_BIT, attr);
235   }
236
237   if (mask & GL_CURRENT_BIT) {
238      struct gl_current_attrib *attr;
239      FLUSH_CURRENT( ctx, 0 );
240      attr = MALLOC_STRUCT( gl_current_attrib );
241      memcpy( attr, &ctx->Current, sizeof(struct gl_current_attrib) );
242      save_attrib_data(&head, GL_CURRENT_BIT, attr);
243   }
244
245   if (mask & GL_DEPTH_BUFFER_BIT) {
246      struct gl_depthbuffer_attrib *attr;
247      attr = MALLOC_STRUCT( gl_depthbuffer_attrib );
248      memcpy( attr, &ctx->Depth, sizeof(struct gl_depthbuffer_attrib) );
249      save_attrib_data(&head, GL_DEPTH_BUFFER_BIT, attr);
250   }
251
252   if (mask & GL_ENABLE_BIT) {
253      struct gl_enable_attrib *attr;
254      GLuint i;
255      attr = MALLOC_STRUCT( gl_enable_attrib );
256      /* Copy enable flags from all other attributes into the enable struct. */
257      attr->AlphaTest = ctx->Color.AlphaEnabled;
258      attr->AutoNormal = ctx->Eval.AutoNormal;
259      attr->Blend = ctx->Color.BlendEnabled;
260      attr->ClipPlanes = ctx->Transform.ClipPlanesEnabled;
261      attr->ColorMaterial = ctx->Light.ColorMaterialEnabled;
262      attr->Convolution1D = ctx->Pixel.Convolution1DEnabled;
263      attr->Convolution2D = ctx->Pixel.Convolution2DEnabled;
264      attr->Separable2D = ctx->Pixel.Separable2DEnabled;
265      attr->CullFace = ctx->Polygon.CullFlag;
266      attr->DepthClamp = ctx->Transform.DepthClamp;
267      attr->DepthTest = ctx->Depth.Test;
268      attr->Dither = ctx->Color.DitherFlag;
269      attr->Fog = ctx->Fog.Enabled;
270      for (i = 0; i < ctx->Const.MaxLights; i++) {
271         attr->Light[i] = ctx->Light.Light[i].Enabled;
272      }
273      attr->Lighting = ctx->Light.Enabled;
274      attr->LineSmooth = ctx->Line.SmoothFlag;
275      attr->LineStipple = ctx->Line.StippleFlag;
276      attr->IndexLogicOp = ctx->Color.IndexLogicOpEnabled;
277      attr->ColorLogicOp = ctx->Color.ColorLogicOpEnabled;
278      attr->Map1Color4 = ctx->Eval.Map1Color4;
279      attr->Map1Index = ctx->Eval.Map1Index;
280      attr->Map1Normal = ctx->Eval.Map1Normal;
281      attr->Map1TextureCoord1 = ctx->Eval.Map1TextureCoord1;
282      attr->Map1TextureCoord2 = ctx->Eval.Map1TextureCoord2;
283      attr->Map1TextureCoord3 = ctx->Eval.Map1TextureCoord3;
284      attr->Map1TextureCoord4 = ctx->Eval.Map1TextureCoord4;
285      attr->Map1Vertex3 = ctx->Eval.Map1Vertex3;
286      attr->Map1Vertex4 = ctx->Eval.Map1Vertex4;
287      memcpy(attr->Map1Attrib, ctx->Eval.Map1Attrib, sizeof(ctx->Eval.Map1Attrib));
288      attr->Map2Color4 = ctx->Eval.Map2Color4;
289      attr->Map2Index = ctx->Eval.Map2Index;
290      attr->Map2Normal = ctx->Eval.Map2Normal;
291      attr->Map2TextureCoord1 = ctx->Eval.Map2TextureCoord1;
292      attr->Map2TextureCoord2 = ctx->Eval.Map2TextureCoord2;
293      attr->Map2TextureCoord3 = ctx->Eval.Map2TextureCoord3;
294      attr->Map2TextureCoord4 = ctx->Eval.Map2TextureCoord4;
295      attr->Map2Vertex3 = ctx->Eval.Map2Vertex3;
296      attr->Map2Vertex4 = ctx->Eval.Map2Vertex4;
297      memcpy(attr->Map2Attrib, ctx->Eval.Map2Attrib, sizeof(ctx->Eval.Map2Attrib));
298      attr->Normalize = ctx->Transform.Normalize;
299      attr->RasterPositionUnclipped = ctx->Transform.RasterPositionUnclipped;
300      attr->PointSmooth = ctx->Point.SmoothFlag;
301      attr->PointSprite = ctx->Point.PointSprite;
302      attr->PolygonOffsetPoint = ctx->Polygon.OffsetPoint;
303      attr->PolygonOffsetLine = ctx->Polygon.OffsetLine;
304      attr->PolygonOffsetFill = ctx->Polygon.OffsetFill;
305      attr->PolygonSmooth = ctx->Polygon.SmoothFlag;
306      attr->PolygonStipple = ctx->Polygon.StippleFlag;
307      attr->RescaleNormals = ctx->Transform.RescaleNormals;
308      attr->Scissor = ctx->Scissor.Enabled;
309      attr->Stencil = ctx->Stencil.Enabled;
310      attr->StencilTwoSide = ctx->Stencil.TestTwoSide;
311      attr->MultisampleEnabled = ctx->Multisample.Enabled;
312      attr->SampleAlphaToCoverage = ctx->Multisample.SampleAlphaToCoverage;
313      attr->SampleAlphaToOne = ctx->Multisample.SampleAlphaToOne;
314      attr->SampleCoverage = ctx->Multisample.SampleCoverage;
315      attr->SampleCoverageInvert = ctx->Multisample.SampleCoverageInvert;
316      for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
317         attr->Texture[i] = ctx->Texture.Unit[i].Enabled;
318         attr->TexGen[i] = ctx->Texture.Unit[i].TexGenEnabled;
319         attr->TextureColorTable[i] = ctx->Texture.Unit[i].ColorTableEnabled;
320      }
321      /* GL_NV_vertex_program */
322      attr->VertexProgram = ctx->VertexProgram.Enabled;
323      attr->VertexProgramPointSize = ctx->VertexProgram.PointSizeEnabled;
324      attr->VertexProgramTwoSide = ctx->VertexProgram.TwoSideEnabled;
325      save_attrib_data(&head, GL_ENABLE_BIT, attr);
326   }
327
328   if (mask & GL_EVAL_BIT) {
329      struct gl_eval_attrib *attr;
330      attr = MALLOC_STRUCT( gl_eval_attrib );
331      memcpy( attr, &ctx->Eval, sizeof(struct gl_eval_attrib) );
332      save_attrib_data(&head, GL_EVAL_BIT, attr);
333   }
334
335   if (mask & GL_FOG_BIT) {
336      struct gl_fog_attrib *attr;
337      attr = MALLOC_STRUCT( gl_fog_attrib );
338      memcpy( attr, &ctx->Fog, sizeof(struct gl_fog_attrib) );
339      save_attrib_data(&head, GL_FOG_BIT, attr);
340   }
341
342   if (mask & GL_HINT_BIT) {
343      struct gl_hint_attrib *attr;
344      attr = MALLOC_STRUCT( gl_hint_attrib );
345      memcpy( attr, &ctx->Hint, sizeof(struct gl_hint_attrib) );
346      save_attrib_data(&head, GL_HINT_BIT, attr);
347   }
348
349   if (mask & GL_LIGHTING_BIT) {
350      struct gl_light_attrib *attr;
351      FLUSH_CURRENT(ctx, 0);	/* flush material changes */
352      attr = MALLOC_STRUCT( gl_light_attrib );
353      memcpy( attr, &ctx->Light, sizeof(struct gl_light_attrib) );
354      save_attrib_data(&head, GL_LIGHTING_BIT, attr);
355   }
356
357   if (mask & GL_LINE_BIT) {
358      struct gl_line_attrib *attr;
359      attr = MALLOC_STRUCT( gl_line_attrib );
360      memcpy( attr, &ctx->Line, sizeof(struct gl_line_attrib) );
361      save_attrib_data(&head, GL_LINE_BIT, attr);
362   }
363
364   if (mask & GL_LIST_BIT) {
365      struct gl_list_attrib *attr;
366      attr = MALLOC_STRUCT( gl_list_attrib );
367      memcpy( attr, &ctx->List, sizeof(struct gl_list_attrib) );
368      save_attrib_data(&head, GL_LIST_BIT, attr);
369   }
370
371   if (mask & GL_PIXEL_MODE_BIT) {
372      struct gl_pixel_attrib *attr;
373      attr = MALLOC_STRUCT( gl_pixel_attrib );
374      memcpy( attr, &ctx->Pixel, sizeof(struct gl_pixel_attrib) );
375      /* push the Read FBO's ReadBuffer state, not ctx->Pixel.ReadBuffer */
376      attr->ReadBuffer = ctx->ReadBuffer->ColorReadBuffer;
377      save_attrib_data(&head, GL_PIXEL_MODE_BIT, attr);
378   }
379
380   if (mask & GL_POINT_BIT) {
381      struct gl_point_attrib *attr;
382      attr = MALLOC_STRUCT( gl_point_attrib );
383      memcpy( attr, &ctx->Point, sizeof(struct gl_point_attrib) );
384      save_attrib_data(&head, GL_POINT_BIT, attr);
385   }
386
387   if (mask & GL_POLYGON_BIT) {
388      struct gl_polygon_attrib *attr;
389      attr = MALLOC_STRUCT( gl_polygon_attrib );
390      memcpy( attr, &ctx->Polygon, sizeof(struct gl_polygon_attrib) );
391      save_attrib_data(&head, GL_POLYGON_BIT, attr);
392   }
393
394   if (mask & GL_POLYGON_STIPPLE_BIT) {
395      GLuint *stipple;
396      stipple = (GLuint *) MALLOC( 32*sizeof(GLuint) );
397      memcpy( stipple, ctx->PolygonStipple, 32*sizeof(GLuint) );
398      save_attrib_data(&head, GL_POLYGON_STIPPLE_BIT, stipple);
399   }
400
401   if (mask & GL_SCISSOR_BIT) {
402      struct gl_scissor_attrib *attr;
403      attr = MALLOC_STRUCT( gl_scissor_attrib );
404      memcpy( attr, &ctx->Scissor, sizeof(struct gl_scissor_attrib) );
405      save_attrib_data(&head, GL_SCISSOR_BIT, attr);
406   }
407
408   if (mask & GL_STENCIL_BUFFER_BIT) {
409      struct gl_stencil_attrib *attr;
410      attr = MALLOC_STRUCT( gl_stencil_attrib );
411      memcpy( attr, &ctx->Stencil, sizeof(struct gl_stencil_attrib) );
412      save_attrib_data(&head, GL_STENCIL_BUFFER_BIT, attr);
413   }
414
415   if (mask & GL_TEXTURE_BIT) {
416      struct texture_state *texstate = CALLOC_STRUCT(texture_state);
417      GLuint u, tex;
418
419      if (!texstate) {
420         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib(GL_TEXTURE_BIT)");
421         goto end;
422      }
423
424      _mesa_lock_context_textures(ctx);
425
426      /* copy/save the bulk of texture state here */
427      memcpy(&texstate->Texture, &ctx->Texture, sizeof(ctx->Texture));
428
429      /* Save references to the currently bound texture objects so they don't
430       * accidentally get deleted while referenced in the attribute stack.
431       */
432      for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
433         for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
434            _mesa_reference_texobj(&texstate->SavedTexRef[u][tex],
435                                   ctx->Texture.Unit[u].CurrentTex[tex]);
436         }
437      }
438
439      /* copy state/contents of the currently bound texture objects */
440      for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
441         for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
442            _mesa_copy_texture_object(&texstate->SavedObj[u][tex],
443                                      ctx->Texture.Unit[u].CurrentTex[tex]);
444         }
445      }
446
447      _mesa_unlock_context_textures(ctx);
448
449      save_attrib_data(&head, GL_TEXTURE_BIT, texstate);
450   }
451
452   if (mask & GL_TRANSFORM_BIT) {
453      struct gl_transform_attrib *attr;
454      attr = MALLOC_STRUCT( gl_transform_attrib );
455      memcpy( attr, &ctx->Transform, sizeof(struct gl_transform_attrib) );
456      save_attrib_data(&head, GL_TRANSFORM_BIT, attr);
457   }
458
459   if (mask & GL_VIEWPORT_BIT) {
460      struct gl_viewport_attrib *attr;
461      attr = MALLOC_STRUCT( gl_viewport_attrib );
462      memcpy( attr, &ctx->Viewport, sizeof(struct gl_viewport_attrib) );
463      save_attrib_data(&head, GL_VIEWPORT_BIT, attr);
464   }
465
466   /* GL_ARB_multisample */
467   if (mask & GL_MULTISAMPLE_BIT_ARB) {
468      struct gl_multisample_attrib *attr;
469      attr = MALLOC_STRUCT( gl_multisample_attrib );
470      memcpy( attr, &ctx->Multisample, sizeof(struct gl_multisample_attrib) );
471      save_attrib_data(&head, GL_MULTISAMPLE_BIT_ARB, attr);
472   }
473
474end:
475   ctx->AttribStack[ctx->AttribStackDepth] = head;
476   ctx->AttribStackDepth++;
477}
478
479
480
481static void
482pop_enable_group(GLcontext *ctx, const struct gl_enable_attrib *enable)
483{
484   const GLuint curTexUnitSave = ctx->Texture.CurrentUnit;
485   GLuint i;
486
487#define TEST_AND_UPDATE(VALUE, NEWVALUE, ENUM)		\
488	if ((VALUE) != (NEWVALUE)) {			\
489	   _mesa_set_enable( ctx, ENUM, (NEWVALUE) );	\
490	}
491
492   TEST_AND_UPDATE(ctx->Color.AlphaEnabled, enable->AlphaTest, GL_ALPHA_TEST);
493   if (ctx->Color.BlendEnabled != enable->Blend) {
494      if (ctx->Extensions.EXT_draw_buffers2) {
495         GLuint i;
496         for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
497            _mesa_set_enablei(ctx, GL_BLEND, i, (enable->Blend >> i) & 1);
498         }
499      }
500      else {
501         _mesa_set_enable(ctx, GL_BLEND, (enable->Blend & 1));
502      }
503   }
504
505   for (i=0;i<MAX_CLIP_PLANES;i++) {
506      const GLuint mask = 1 << i;
507      if ((ctx->Transform.ClipPlanesEnabled & mask) != (enable->ClipPlanes & mask))
508	  _mesa_set_enable(ctx, (GLenum) (GL_CLIP_PLANE0 + i),
509			   (GLboolean) ((enable->ClipPlanes & mask) ? GL_TRUE : GL_FALSE));
510   }
511
512   TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled, enable->ColorMaterial,
513                   GL_COLOR_MATERIAL);
514   TEST_AND_UPDATE(ctx->Polygon.CullFlag, enable->CullFace, GL_CULL_FACE);
515   TEST_AND_UPDATE(ctx->Transform.DepthClamp, enable->DepthClamp,
516		   GL_DEPTH_CLAMP);
517   TEST_AND_UPDATE(ctx->Depth.Test, enable->DepthTest, GL_DEPTH_TEST);
518   TEST_AND_UPDATE(ctx->Color.DitherFlag, enable->Dither, GL_DITHER);
519   TEST_AND_UPDATE(ctx->Pixel.Convolution1DEnabled, enable->Convolution1D,
520                   GL_CONVOLUTION_1D);
521   TEST_AND_UPDATE(ctx->Pixel.Convolution2DEnabled, enable->Convolution2D,
522                   GL_CONVOLUTION_2D);
523   TEST_AND_UPDATE(ctx->Pixel.Separable2DEnabled, enable->Separable2D,
524                   GL_SEPARABLE_2D);
525   TEST_AND_UPDATE(ctx->Fog.Enabled, enable->Fog, GL_FOG);
526   TEST_AND_UPDATE(ctx->Light.Enabled, enable->Lighting, GL_LIGHTING);
527   TEST_AND_UPDATE(ctx->Line.SmoothFlag, enable->LineSmooth, GL_LINE_SMOOTH);
528   TEST_AND_UPDATE(ctx->Line.StippleFlag, enable->LineStipple,
529                   GL_LINE_STIPPLE);
530   TEST_AND_UPDATE(ctx->Color.IndexLogicOpEnabled, enable->IndexLogicOp,
531                   GL_INDEX_LOGIC_OP);
532   TEST_AND_UPDATE(ctx->Color.ColorLogicOpEnabled, enable->ColorLogicOp,
533                   GL_COLOR_LOGIC_OP);
534
535   TEST_AND_UPDATE(ctx->Eval.Map1Color4, enable->Map1Color4, GL_MAP1_COLOR_4);
536   TEST_AND_UPDATE(ctx->Eval.Map1Index, enable->Map1Index, GL_MAP1_INDEX);
537   TEST_AND_UPDATE(ctx->Eval.Map1Normal, enable->Map1Normal, GL_MAP1_NORMAL);
538   TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord1, enable->Map1TextureCoord1,
539                   GL_MAP1_TEXTURE_COORD_1);
540   TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord2, enable->Map1TextureCoord2,
541                   GL_MAP1_TEXTURE_COORD_2);
542   TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord3, enable->Map1TextureCoord3,
543                   GL_MAP1_TEXTURE_COORD_3);
544   TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord4, enable->Map1TextureCoord4,
545                   GL_MAP1_TEXTURE_COORD_4);
546   TEST_AND_UPDATE(ctx->Eval.Map1Vertex3, enable->Map1Vertex3,
547                   GL_MAP1_VERTEX_3);
548   TEST_AND_UPDATE(ctx->Eval.Map1Vertex4, enable->Map1Vertex4,
549                   GL_MAP1_VERTEX_4);
550   for (i = 0; i < 16; i++) {
551      TEST_AND_UPDATE(ctx->Eval.Map1Attrib[i], enable->Map1Attrib[i],
552                      GL_MAP1_VERTEX_ATTRIB0_4_NV + i);
553   }
554
555   TEST_AND_UPDATE(ctx->Eval.Map2Color4, enable->Map2Color4, GL_MAP2_COLOR_4);
556   TEST_AND_UPDATE(ctx->Eval.Map2Index, enable->Map2Index, GL_MAP2_INDEX);
557   TEST_AND_UPDATE(ctx->Eval.Map2Normal, enable->Map2Normal, GL_MAP2_NORMAL);
558   TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord1, enable->Map2TextureCoord1,
559                   GL_MAP2_TEXTURE_COORD_1);
560   TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord2, enable->Map2TextureCoord2,
561                   GL_MAP2_TEXTURE_COORD_2);
562   TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord3, enable->Map2TextureCoord3,
563                   GL_MAP2_TEXTURE_COORD_3);
564   TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord4, enable->Map2TextureCoord4,
565                   GL_MAP2_TEXTURE_COORD_4);
566   TEST_AND_UPDATE(ctx->Eval.Map2Vertex3, enable->Map2Vertex3,
567                   GL_MAP2_VERTEX_3);
568   TEST_AND_UPDATE(ctx->Eval.Map2Vertex4, enable->Map2Vertex4,
569                   GL_MAP2_VERTEX_4);
570   for (i = 0; i < 16; i++) {
571      TEST_AND_UPDATE(ctx->Eval.Map2Attrib[i], enable->Map2Attrib[i],
572                      GL_MAP2_VERTEX_ATTRIB0_4_NV + i);
573   }
574
575   TEST_AND_UPDATE(ctx->Eval.AutoNormal, enable->AutoNormal, GL_AUTO_NORMAL);
576   TEST_AND_UPDATE(ctx->Transform.Normalize, enable->Normalize, GL_NORMALIZE);
577   TEST_AND_UPDATE(ctx->Transform.RescaleNormals, enable->RescaleNormals,
578                   GL_RESCALE_NORMAL_EXT);
579   TEST_AND_UPDATE(ctx->Transform.RasterPositionUnclipped,
580                   enable->RasterPositionUnclipped,
581                   GL_RASTER_POSITION_UNCLIPPED_IBM);
582   TEST_AND_UPDATE(ctx->Point.SmoothFlag, enable->PointSmooth,
583                   GL_POINT_SMOOTH);
584   if (ctx->Extensions.NV_point_sprite || ctx->Extensions.ARB_point_sprite) {
585      TEST_AND_UPDATE(ctx->Point.PointSprite, enable->PointSprite,
586                      GL_POINT_SPRITE_NV);
587   }
588   TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, enable->PolygonOffsetPoint,
589                   GL_POLYGON_OFFSET_POINT);
590   TEST_AND_UPDATE(ctx->Polygon.OffsetLine, enable->PolygonOffsetLine,
591                   GL_POLYGON_OFFSET_LINE);
592   TEST_AND_UPDATE(ctx->Polygon.OffsetFill, enable->PolygonOffsetFill,
593                   GL_POLYGON_OFFSET_FILL);
594   TEST_AND_UPDATE(ctx->Polygon.SmoothFlag, enable->PolygonSmooth,
595                   GL_POLYGON_SMOOTH);
596   TEST_AND_UPDATE(ctx->Polygon.StippleFlag, enable->PolygonStipple,
597                   GL_POLYGON_STIPPLE);
598   TEST_AND_UPDATE(ctx->Scissor.Enabled, enable->Scissor, GL_SCISSOR_TEST);
599   TEST_AND_UPDATE(ctx->Stencil.Enabled, enable->Stencil, GL_STENCIL_TEST);
600   if (ctx->Extensions.EXT_stencil_two_side) {
601      TEST_AND_UPDATE(ctx->Stencil.TestTwoSide, enable->StencilTwoSide, GL_STENCIL_TEST_TWO_SIDE_EXT);
602   }
603   TEST_AND_UPDATE(ctx->Multisample.Enabled, enable->MultisampleEnabled,
604                   GL_MULTISAMPLE_ARB);
605   TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
606                   enable->SampleAlphaToCoverage,
607                   GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
608   TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
609                   enable->SampleAlphaToOne,
610                   GL_SAMPLE_ALPHA_TO_ONE_ARB);
611   TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
612                   enable->SampleCoverage,
613                   GL_SAMPLE_COVERAGE_ARB);
614   TEST_AND_UPDATE(ctx->Multisample.SampleCoverageInvert,
615                   enable->SampleCoverageInvert,
616                   GL_SAMPLE_COVERAGE_INVERT_ARB);
617   /* GL_ARB_vertex_program, GL_NV_vertex_program */
618   TEST_AND_UPDATE(ctx->VertexProgram.Enabled,
619                   enable->VertexProgram,
620                   GL_VERTEX_PROGRAM_ARB);
621   TEST_AND_UPDATE(ctx->VertexProgram.PointSizeEnabled,
622                   enable->VertexProgramPointSize,
623                   GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
624   TEST_AND_UPDATE(ctx->VertexProgram.TwoSideEnabled,
625                   enable->VertexProgramTwoSide,
626                   GL_VERTEX_PROGRAM_TWO_SIDE_ARB);
627
628#undef TEST_AND_UPDATE
629
630   /* texture unit enables */
631   for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
632      const GLbitfield enabled = enable->Texture[i];
633      const GLbitfield genEnabled = enable->TexGen[i];
634
635      if (ctx->Texture.Unit[i].Enabled != enabled) {
636         _mesa_ActiveTextureARB(GL_TEXTURE0 + i);
637
638         _mesa_set_enable(ctx, GL_TEXTURE_1D,
639                          (enabled & TEXTURE_1D_BIT) ? GL_TRUE : GL_FALSE);
640         _mesa_set_enable(ctx, GL_TEXTURE_2D,
641                          (enabled & TEXTURE_2D_BIT) ? GL_TRUE : GL_FALSE);
642         _mesa_set_enable(ctx, GL_TEXTURE_3D,
643                          (enabled & TEXTURE_3D_BIT) ? GL_TRUE : GL_FALSE);
644         if (ctx->Extensions.NV_texture_rectangle) {
645            _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE_ARB,
646                             (enabled & TEXTURE_RECT_BIT) ? GL_TRUE : GL_FALSE);
647         }
648         if (ctx->Extensions.ARB_texture_cube_map) {
649            _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP,
650                             (enabled & TEXTURE_CUBE_BIT) ? GL_TRUE : GL_FALSE);
651         }
652         if (ctx->Extensions.MESA_texture_array) {
653            _mesa_set_enable(ctx, GL_TEXTURE_1D_ARRAY_EXT,
654                           (enabled & TEXTURE_1D_ARRAY_BIT) ? GL_TRUE : GL_FALSE);
655            _mesa_set_enable(ctx, GL_TEXTURE_2D_ARRAY_EXT,
656                           (enabled & TEXTURE_2D_ARRAY_BIT) ? GL_TRUE : GL_FALSE);
657         }
658      }
659
660      if (ctx->Texture.Unit[i].TexGenEnabled != genEnabled) {
661         _mesa_ActiveTextureARB(GL_TEXTURE0 + i);
662         _mesa_set_enable(ctx, GL_TEXTURE_GEN_S,
663                          (genEnabled & S_BIT) ? GL_TRUE : GL_FALSE);
664         _mesa_set_enable(ctx, GL_TEXTURE_GEN_T,
665                          (genEnabled & T_BIT) ? GL_TRUE : GL_FALSE);
666         _mesa_set_enable(ctx, GL_TEXTURE_GEN_R,
667                          (genEnabled & R_BIT) ? GL_TRUE : GL_FALSE);
668         _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q,
669                          (genEnabled & Q_BIT) ? GL_TRUE : GL_FALSE);
670      }
671
672      /* GL_SGI_texture_color_table */
673      ctx->Texture.Unit[i].ColorTableEnabled = enable->TextureColorTable[i];
674   }
675
676   _mesa_ActiveTextureARB(GL_TEXTURE0 + curTexUnitSave);
677}
678
679
680/**
681 * Pop/restore texture attribute/group state.
682 */
683static void
684pop_texture_group(GLcontext *ctx, struct texture_state *texstate)
685{
686   GLuint u;
687
688   _mesa_lock_context_textures(ctx);
689
690   for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
691      const struct gl_texture_unit *unit = &texstate->Texture.Unit[u];
692      GLuint tgt;
693
694      _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + u);
695      _mesa_set_enable(ctx, GL_TEXTURE_1D,
696                       (unit->Enabled & TEXTURE_1D_BIT) ? GL_TRUE : GL_FALSE);
697      _mesa_set_enable(ctx, GL_TEXTURE_2D,
698                       (unit->Enabled & TEXTURE_2D_BIT) ? GL_TRUE : GL_FALSE);
699      _mesa_set_enable(ctx, GL_TEXTURE_3D,
700                       (unit->Enabled & TEXTURE_3D_BIT) ? GL_TRUE : GL_FALSE);
701      if (ctx->Extensions.ARB_texture_cube_map) {
702         _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP_ARB,
703                     (unit->Enabled & TEXTURE_CUBE_BIT) ? GL_TRUE : GL_FALSE);
704      }
705      if (ctx->Extensions.NV_texture_rectangle) {
706         _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE_NV,
707                     (unit->Enabled & TEXTURE_RECT_BIT) ? GL_TRUE : GL_FALSE);
708      }
709      if (ctx->Extensions.MESA_texture_array) {
710         _mesa_set_enable(ctx, GL_TEXTURE_1D_ARRAY_EXT,
711                 (unit->Enabled & TEXTURE_1D_ARRAY_BIT) ? GL_TRUE : GL_FALSE);
712         _mesa_set_enable(ctx, GL_TEXTURE_2D_ARRAY_EXT,
713                 (unit->Enabled & TEXTURE_2D_ARRAY_BIT) ? GL_TRUE : GL_FALSE);
714      }
715
716      if (ctx->Extensions.SGI_texture_color_table) {
717         _mesa_set_enable(ctx, GL_TEXTURE_COLOR_TABLE_SGI,
718                          unit->ColorTableEnabled);
719      }
720      _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->EnvMode);
721      _mesa_TexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, unit->EnvColor);
722      _mesa_TexGeni(GL_S, GL_TEXTURE_GEN_MODE, unit->GenS.Mode);
723      _mesa_TexGeni(GL_T, GL_TEXTURE_GEN_MODE, unit->GenT.Mode);
724      _mesa_TexGeni(GL_R, GL_TEXTURE_GEN_MODE, unit->GenR.Mode);
725      _mesa_TexGeni(GL_Q, GL_TEXTURE_GEN_MODE, unit->GenQ.Mode);
726      _mesa_TexGenfv(GL_S, GL_OBJECT_PLANE, unit->GenS.ObjectPlane);
727      _mesa_TexGenfv(GL_T, GL_OBJECT_PLANE, unit->GenT.ObjectPlane);
728      _mesa_TexGenfv(GL_R, GL_OBJECT_PLANE, unit->GenR.ObjectPlane);
729      _mesa_TexGenfv(GL_Q, GL_OBJECT_PLANE, unit->GenQ.ObjectPlane);
730      /* Eye plane done differently to avoid re-transformation */
731      {
732         struct gl_texture_unit *destUnit = &ctx->Texture.Unit[u];
733         COPY_4FV(destUnit->GenS.EyePlane, unit->GenS.EyePlane);
734         COPY_4FV(destUnit->GenT.EyePlane, unit->GenT.EyePlane);
735         COPY_4FV(destUnit->GenR.EyePlane, unit->GenR.EyePlane);
736         COPY_4FV(destUnit->GenQ.EyePlane, unit->GenQ.EyePlane);
737         if (ctx->Driver.TexGen) {
738            ctx->Driver.TexGen(ctx, GL_S, GL_EYE_PLANE, unit->GenS.EyePlane);
739            ctx->Driver.TexGen(ctx, GL_T, GL_EYE_PLANE, unit->GenT.EyePlane);
740            ctx->Driver.TexGen(ctx, GL_R, GL_EYE_PLANE, unit->GenR.EyePlane);
741            ctx->Driver.TexGen(ctx, GL_Q, GL_EYE_PLANE, unit->GenQ.EyePlane);
742         }
743      }
744      _mesa_set_enable(ctx, GL_TEXTURE_GEN_S,
745                       ((unit->TexGenEnabled & S_BIT) ? GL_TRUE : GL_FALSE));
746      _mesa_set_enable(ctx, GL_TEXTURE_GEN_T,
747                       ((unit->TexGenEnabled & T_BIT) ? GL_TRUE : GL_FALSE));
748      _mesa_set_enable(ctx, GL_TEXTURE_GEN_R,
749                       ((unit->TexGenEnabled & R_BIT) ? GL_TRUE : GL_FALSE));
750      _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q,
751                       ((unit->TexGenEnabled & Q_BIT) ? GL_TRUE : GL_FALSE));
752      if (ctx->Extensions.EXT_texture_lod_bias) {
753         _mesa_TexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
754                       GL_TEXTURE_LOD_BIAS_EXT, unit->LodBias);
755      }
756      if (ctx->Extensions.EXT_texture_env_combine ||
757          ctx->Extensions.ARB_texture_env_combine) {
758         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,
759                       unit->Combine.ModeRGB);
760         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,
761                       unit->Combine.ModeA);
762         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB,
763                       unit->Combine.SourceRGB[0]);
764         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB,
765                       unit->Combine.SourceRGB[1]);
766         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB,
767                       unit->Combine.SourceRGB[2]);
768         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA,
769                       unit->Combine.SourceA[0]);
770         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA,
771                       unit->Combine.SourceA[1]);
772         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA,
773                       unit->Combine.SourceA[2]);
774         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB,
775                       unit->Combine.OperandRGB[0]);
776         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB,
777                       unit->Combine.OperandRGB[1]);
778         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB,
779                       unit->Combine.OperandRGB[2]);
780         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA,
781                       unit->Combine.OperandA[0]);
782         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA,
783                       unit->Combine.OperandA[1]);
784         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA,
785                       unit->Combine.OperandA[2]);
786         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE,
787                       1 << unit->Combine.ScaleShiftRGB);
788         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE,
789                       1 << unit->Combine.ScaleShiftA);
790      }
791
792      /* Restore texture object state for each target */
793      for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
794         const struct gl_texture_object *obj = NULL;
795         GLenum target;
796
797         obj = &texstate->SavedObj[u][tgt];
798
799         /* don't restore state for unsupported targets to prevent
800          * raising GL errors.
801          */
802         if (obj->Target == GL_TEXTURE_CUBE_MAP_ARB &&
803             !ctx->Extensions.ARB_texture_cube_map) {
804            continue;
805         }
806         else if (obj->Target == GL_TEXTURE_RECTANGLE_NV &&
807                  !ctx->Extensions.NV_texture_rectangle) {
808            continue;
809         }
810         else if ((obj->Target == GL_TEXTURE_1D_ARRAY_EXT ||
811                   obj->Target == GL_TEXTURE_2D_ARRAY_EXT) &&
812                  !ctx->Extensions.MESA_texture_array) {
813            continue;
814         }
815
816         target = obj->Target;
817
818         _mesa_BindTexture(target, obj->Name);
819
820         _mesa_TexParameterfv(target, GL_TEXTURE_BORDER_COLOR, obj->BorderColor.f);
821         _mesa_TexParameterf(target, GL_TEXTURE_PRIORITY, obj->Priority);
822         _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, obj->WrapS);
823         _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, obj->WrapT);
824         _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, obj->WrapR);
825         _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, obj->MinFilter);
826         _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, obj->MagFilter);
827         _mesa_TexParameterf(target, GL_TEXTURE_MIN_LOD, obj->MinLod);
828         _mesa_TexParameterf(target, GL_TEXTURE_MAX_LOD, obj->MaxLod);
829         _mesa_TexParameterf(target, GL_TEXTURE_LOD_BIAS, obj->LodBias);
830         _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, obj->BaseLevel);
831         if (target != GL_TEXTURE_RECTANGLE_ARB)
832            _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, obj->MaxLevel);
833         if (ctx->Extensions.EXT_texture_filter_anisotropic) {
834            _mesa_TexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
835                                obj->MaxAnisotropy);
836         }
837         if (ctx->Extensions.ARB_shadow_ambient) {
838            _mesa_TexParameterf(target, GL_TEXTURE_COMPARE_FAIL_VALUE_ARB,
839                                obj->CompareFailValue);
840         }
841      }
842
843      /* remove saved references to the texture objects */
844      for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
845         _mesa_reference_texobj(&texstate->SavedTexRef[u][tgt], NULL);
846      }
847   }
848
849   _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + texstate->Texture.CurrentUnit);
850
851   _mesa_unlock_context_textures(ctx);
852}
853
854
855/*
856 * This function is kind of long just because we have to call a lot
857 * of device driver functions to update device driver state.
858 *
859 * XXX As it is now, most of the pop-code calls immediate-mode Mesa functions
860 * in order to restore GL state.  This isn't terribly efficient but it
861 * ensures that dirty flags and any derived state gets updated correctly.
862 * We could at least check if the value to restore equals the current value
863 * and then skip the Mesa call.
864 */
865void GLAPIENTRY
866_mesa_PopAttrib(void)
867{
868   struct gl_attrib_node *attr, *next;
869   GET_CURRENT_CONTEXT(ctx);
870   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
871
872   if (ctx->AttribStackDepth == 0) {
873      _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopAttrib" );
874      return;
875   }
876
877   ctx->AttribStackDepth--;
878   attr = ctx->AttribStack[ctx->AttribStackDepth];
879
880   while (attr) {
881
882      if (MESA_VERBOSE & VERBOSE_API) {
883         _mesa_debug(ctx, "glPopAttrib %s\n",
884                     _mesa_lookup_enum_by_nr(attr->kind));
885      }
886
887      switch (attr->kind) {
888         case GL_ACCUM_BUFFER_BIT:
889            {
890               const struct gl_accum_attrib *accum;
891               accum = (const struct gl_accum_attrib *) attr->data;
892               _mesa_ClearAccum(accum->ClearColor[0],
893                                accum->ClearColor[1],
894                                accum->ClearColor[2],
895                                accum->ClearColor[3]);
896            }
897            break;
898         case GL_COLOR_BUFFER_BIT:
899            {
900               const struct gl_colorbuffer_attrib *color;
901
902               color = (const struct gl_colorbuffer_attrib *) attr->data;
903               _mesa_ClearIndex((GLfloat) color->ClearIndex);
904               _mesa_ClearColor(color->ClearColor[0],
905                                color->ClearColor[1],
906                                color->ClearColor[2],
907                                color->ClearColor[3]);
908               _mesa_IndexMask(color->IndexMask);
909               if (!ctx->Extensions.EXT_draw_buffers2) {
910                  _mesa_ColorMask((GLboolean) (color->ColorMask[0][0] != 0),
911                                  (GLboolean) (color->ColorMask[0][1] != 0),
912                                  (GLboolean) (color->ColorMask[0][2] != 0),
913                                  (GLboolean) (color->ColorMask[0][3] != 0));
914               }
915               else {
916                  GLuint i;
917                  for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
918                     _mesa_ColorMaskIndexed(i,
919                                  (GLboolean) (color->ColorMask[i][0] != 0),
920                                  (GLboolean) (color->ColorMask[i][1] != 0),
921                                  (GLboolean) (color->ColorMask[i][2] != 0),
922                                  (GLboolean) (color->ColorMask[i][3] != 0));
923                  }
924               }
925               {
926                  /* Need to determine if more than one color output is
927                   * specified.  If so, call glDrawBuffersARB, else call
928                   * glDrawBuffer().  This is a subtle, but essential point
929                   * since GL_FRONT (for example) is illegal for the former
930                   * function, but legal for the later.
931                   */
932                  GLboolean multipleBuffers = GL_FALSE;
933		  GLuint i;
934
935		  for (i = 1; i < ctx->Const.MaxDrawBuffers; i++) {
936		     if (color->DrawBuffer[i] != GL_NONE) {
937			multipleBuffers = GL_TRUE;
938			break;
939		     }
940                  }
941                  /* Call the API_level functions, not _mesa_drawbuffers()
942                   * since we need to do error checking on the pop'd
943                   * GL_DRAW_BUFFER.
944                   * Ex: if GL_FRONT were pushed, but we're popping with a
945                   * user FBO bound, GL_FRONT will be illegal and we'll need
946                   * to record that error.  Per OpenGL ARB decision.
947                   */
948                  if (multipleBuffers)
949                     _mesa_DrawBuffersARB(ctx->Const.MaxDrawBuffers,
950                                          color->DrawBuffer);
951                  else
952                     _mesa_DrawBuffer(color->DrawBuffer[0]);
953               }
954               _mesa_set_enable(ctx, GL_ALPHA_TEST, color->AlphaEnabled);
955               _mesa_AlphaFunc(color->AlphaFunc, color->AlphaRef);
956               if (ctx->Color.BlendEnabled != color->BlendEnabled) {
957                  if (ctx->Extensions.EXT_draw_buffers2) {
958                     GLuint i;
959                     for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
960                        _mesa_set_enablei(ctx, GL_BLEND, i,
961                                          (color->BlendEnabled >> i) & 1);
962                     }
963                  }
964                  else {
965                     _mesa_set_enable(ctx, GL_BLEND, (color->BlendEnabled & 1));
966                  }
967               }
968               _mesa_BlendFuncSeparateEXT(color->BlendSrcRGB,
969                                          color->BlendDstRGB,
970                                          color->BlendSrcA,
971                                          color->BlendDstA);
972	       /* This special case is because glBlendEquationSeparateEXT
973		* cannot take GL_LOGIC_OP as a parameter.
974		*/
975	       if ( color->BlendEquationRGB == color->BlendEquationA ) {
976		  _mesa_BlendEquation(color->BlendEquationRGB);
977	       }
978	       else {
979		  _mesa_BlendEquationSeparateEXT(color->BlendEquationRGB,
980						 color->BlendEquationA);
981	       }
982               _mesa_BlendColor(color->BlendColor[0],
983                                color->BlendColor[1],
984                                color->BlendColor[2],
985                                color->BlendColor[3]);
986               _mesa_LogicOp(color->LogicOp);
987               _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP,
988                                color->ColorLogicOpEnabled);
989               _mesa_set_enable(ctx, GL_INDEX_LOGIC_OP,
990                                color->IndexLogicOpEnabled);
991               _mesa_set_enable(ctx, GL_DITHER, color->DitherFlag);
992            }
993            break;
994         case GL_CURRENT_BIT:
995	    FLUSH_CURRENT( ctx, 0 );
996            memcpy( &ctx->Current, attr->data,
997		    sizeof(struct gl_current_attrib) );
998            break;
999         case GL_DEPTH_BUFFER_BIT:
1000            {
1001               const struct gl_depthbuffer_attrib *depth;
1002               depth = (const struct gl_depthbuffer_attrib *) attr->data;
1003               _mesa_DepthFunc(depth->Func);
1004               _mesa_ClearDepth(depth->Clear);
1005               _mesa_set_enable(ctx, GL_DEPTH_TEST, depth->Test);
1006               _mesa_DepthMask(depth->Mask);
1007            }
1008            break;
1009         case GL_ENABLE_BIT:
1010            {
1011               const struct gl_enable_attrib *enable;
1012               enable = (const struct gl_enable_attrib *) attr->data;
1013               pop_enable_group(ctx, enable);
1014	       ctx->NewState |= _NEW_ALL;
1015            }
1016            break;
1017         case GL_EVAL_BIT:
1018            memcpy( &ctx->Eval, attr->data, sizeof(struct gl_eval_attrib) );
1019	    ctx->NewState |= _NEW_EVAL;
1020            break;
1021         case GL_FOG_BIT:
1022            {
1023               const struct gl_fog_attrib *fog;
1024               fog = (const struct gl_fog_attrib *) attr->data;
1025               _mesa_set_enable(ctx, GL_FOG, fog->Enabled);
1026               _mesa_Fogfv(GL_FOG_COLOR, fog->Color);
1027               _mesa_Fogf(GL_FOG_DENSITY, fog->Density);
1028               _mesa_Fogf(GL_FOG_START, fog->Start);
1029               _mesa_Fogf(GL_FOG_END, fog->End);
1030               _mesa_Fogf(GL_FOG_INDEX, fog->Index);
1031               _mesa_Fogi(GL_FOG_MODE, fog->Mode);
1032            }
1033            break;
1034         case GL_HINT_BIT:
1035            {
1036               const struct gl_hint_attrib *hint;
1037               hint = (const struct gl_hint_attrib *) attr->data;
1038               _mesa_Hint(GL_PERSPECTIVE_CORRECTION_HINT,
1039                          hint->PerspectiveCorrection );
1040               _mesa_Hint(GL_POINT_SMOOTH_HINT, hint->PointSmooth);
1041               _mesa_Hint(GL_LINE_SMOOTH_HINT, hint->LineSmooth);
1042               _mesa_Hint(GL_POLYGON_SMOOTH_HINT, hint->PolygonSmooth);
1043               _mesa_Hint(GL_FOG_HINT, hint->Fog);
1044               _mesa_Hint(GL_CLIP_VOLUME_CLIPPING_HINT_EXT,
1045                          hint->ClipVolumeClipping);
1046	       _mesa_Hint(GL_TEXTURE_COMPRESSION_HINT_ARB,
1047			  hint->TextureCompression);
1048            }
1049            break;
1050         case GL_LIGHTING_BIT:
1051            {
1052               GLuint i;
1053               const struct gl_light_attrib *light;
1054               light = (const struct gl_light_attrib *) attr->data;
1055               /* lighting enable */
1056               _mesa_set_enable(ctx, GL_LIGHTING, light->Enabled);
1057               /* per-light state */
1058               if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
1059                  _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
1060
1061               for (i = 0; i < ctx->Const.MaxLights; i++) {
1062                  const struct gl_light *l = &light->Light[i];
1063                  _mesa_set_enable(ctx, GL_LIGHT0 + i, l->Enabled);
1064                  _mesa_light(ctx, i, GL_AMBIENT, l->Ambient);
1065                  _mesa_light(ctx, i, GL_DIFFUSE, l->Diffuse);
1066                  _mesa_light(ctx, i, GL_SPECULAR, l->Specular );
1067                  _mesa_light(ctx, i, GL_POSITION, l->EyePosition);
1068                  _mesa_light(ctx, i, GL_SPOT_DIRECTION, l->SpotDirection);
1069                  {
1070                     GLfloat p[4] = { 0 };
1071                     p[0] = l->SpotExponent;
1072                     _mesa_light(ctx, i, GL_SPOT_EXPONENT, p);
1073                  }
1074                  {
1075                     GLfloat p[4] = { 0 };
1076                     p[0] = l->SpotCutoff;
1077                     _mesa_light(ctx, i, GL_SPOT_CUTOFF, p);
1078                  }
1079                  {
1080                     GLfloat p[4] = { 0 };
1081                     p[0] = l->ConstantAttenuation;
1082                     _mesa_light(ctx, i, GL_CONSTANT_ATTENUATION, p);
1083                  }
1084                  {
1085                     GLfloat p[4] = { 0 };
1086                     p[0] = l->LinearAttenuation;
1087                     _mesa_light(ctx, i, GL_LINEAR_ATTENUATION, p);
1088                  }
1089                  {
1090                     GLfloat p[4] = { 0 };
1091                     p[0] = l->QuadraticAttenuation;
1092                     _mesa_light(ctx, i, GL_QUADRATIC_ATTENUATION, p);
1093                  }
1094                }
1095               /* light model */
1096               _mesa_LightModelfv(GL_LIGHT_MODEL_AMBIENT,
1097                                  light->Model.Ambient);
1098               _mesa_LightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,
1099                                 (GLfloat) light->Model.LocalViewer);
1100               _mesa_LightModelf(GL_LIGHT_MODEL_TWO_SIDE,
1101                                 (GLfloat) light->Model.TwoSide);
1102               _mesa_LightModelf(GL_LIGHT_MODEL_COLOR_CONTROL,
1103                                 (GLfloat) light->Model.ColorControl);
1104               /* shade model */
1105               _mesa_ShadeModel(light->ShadeModel);
1106               /* color material */
1107               _mesa_ColorMaterial(light->ColorMaterialFace,
1108                                   light->ColorMaterialMode);
1109               _mesa_set_enable(ctx, GL_COLOR_MATERIAL,
1110                                light->ColorMaterialEnabled);
1111               /* materials */
1112               memcpy(&ctx->Light.Material, &light->Material,
1113                      sizeof(struct gl_material));
1114            }
1115            break;
1116         case GL_LINE_BIT:
1117            {
1118               const struct gl_line_attrib *line;
1119               line = (const struct gl_line_attrib *) attr->data;
1120               _mesa_set_enable(ctx, GL_LINE_SMOOTH, line->SmoothFlag);
1121               _mesa_set_enable(ctx, GL_LINE_STIPPLE, line->StippleFlag);
1122               _mesa_LineStipple(line->StippleFactor, line->StipplePattern);
1123               _mesa_LineWidth(line->Width);
1124            }
1125            break;
1126         case GL_LIST_BIT:
1127            memcpy( &ctx->List, attr->data, sizeof(struct gl_list_attrib) );
1128            break;
1129         case GL_PIXEL_MODE_BIT:
1130            memcpy( &ctx->Pixel, attr->data, sizeof(struct gl_pixel_attrib) );
1131            /* XXX what other pixel state needs to be set by function calls? */
1132            _mesa_ReadBuffer(ctx->Pixel.ReadBuffer);
1133	    ctx->NewState |= _NEW_PIXEL;
1134            break;
1135         case GL_POINT_BIT:
1136            {
1137               const struct gl_point_attrib *point;
1138               point = (const struct gl_point_attrib *) attr->data;
1139               _mesa_PointSize(point->Size);
1140               _mesa_set_enable(ctx, GL_POINT_SMOOTH, point->SmoothFlag);
1141               if (ctx->Extensions.EXT_point_parameters) {
1142                  _mesa_PointParameterfv(GL_DISTANCE_ATTENUATION_EXT,
1143                                         point->Params);
1144                  _mesa_PointParameterf(GL_POINT_SIZE_MIN_EXT,
1145                                        point->MinSize);
1146                  _mesa_PointParameterf(GL_POINT_SIZE_MAX_EXT,
1147                                        point->MaxSize);
1148                  _mesa_PointParameterf(GL_POINT_FADE_THRESHOLD_SIZE_EXT,
1149                                        point->Threshold);
1150               }
1151               if (ctx->Extensions.NV_point_sprite
1152		   || ctx->Extensions.ARB_point_sprite) {
1153                  GLuint u;
1154                  for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
1155                     _mesa_TexEnvi(GL_POINT_SPRITE_NV, GL_COORD_REPLACE_NV,
1156                                   (GLint) point->CoordReplace[u]);
1157                  }
1158                  _mesa_set_enable(ctx, GL_POINT_SPRITE_NV,point->PointSprite);
1159                  if (ctx->Extensions.NV_point_sprite)
1160                     _mesa_PointParameteri(GL_POINT_SPRITE_R_MODE_NV,
1161                                           ctx->Point.SpriteRMode);
1162                  _mesa_PointParameterf(GL_POINT_SPRITE_COORD_ORIGIN,
1163                                        (GLfloat)ctx->Point.SpriteOrigin);
1164               }
1165            }
1166            break;
1167         case GL_POLYGON_BIT:
1168            {
1169               const struct gl_polygon_attrib *polygon;
1170               polygon = (const struct gl_polygon_attrib *) attr->data;
1171               _mesa_CullFace(polygon->CullFaceMode);
1172               _mesa_FrontFace(polygon->FrontFace);
1173               _mesa_PolygonMode(GL_FRONT, polygon->FrontMode);
1174               _mesa_PolygonMode(GL_BACK, polygon->BackMode);
1175               _mesa_PolygonOffset(polygon->OffsetFactor,
1176                                   polygon->OffsetUnits);
1177               _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, polygon->SmoothFlag);
1178               _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, polygon->StippleFlag);
1179               _mesa_set_enable(ctx, GL_CULL_FACE, polygon->CullFlag);
1180               _mesa_set_enable(ctx, GL_POLYGON_OFFSET_POINT,
1181                                polygon->OffsetPoint);
1182               _mesa_set_enable(ctx, GL_POLYGON_OFFSET_LINE,
1183                                polygon->OffsetLine);
1184               _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL,
1185                                polygon->OffsetFill);
1186            }
1187            break;
1188	 case GL_POLYGON_STIPPLE_BIT:
1189	    memcpy( ctx->PolygonStipple, attr->data, 32*sizeof(GLuint) );
1190	    ctx->NewState |= _NEW_POLYGONSTIPPLE;
1191	    if (ctx->Driver.PolygonStipple)
1192	       ctx->Driver.PolygonStipple( ctx, (const GLubyte *) attr->data );
1193	    break;
1194         case GL_SCISSOR_BIT:
1195            {
1196               const struct gl_scissor_attrib *scissor;
1197               scissor = (const struct gl_scissor_attrib *) attr->data;
1198               _mesa_Scissor(scissor->X, scissor->Y,
1199                             scissor->Width, scissor->Height);
1200               _mesa_set_enable(ctx, GL_SCISSOR_TEST, scissor->Enabled);
1201            }
1202            break;
1203         case GL_STENCIL_BUFFER_BIT:
1204            {
1205               const struct gl_stencil_attrib *stencil;
1206               stencil = (const struct gl_stencil_attrib *) attr->data;
1207               _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
1208               _mesa_ClearStencil(stencil->Clear);
1209               if (ctx->Extensions.EXT_stencil_two_side) {
1210                  _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
1211                                   stencil->TestTwoSide);
1212                  _mesa_ActiveStencilFaceEXT(stencil->ActiveFace
1213                                             ? GL_BACK : GL_FRONT);
1214               }
1215               /* front state */
1216               _mesa_StencilFuncSeparate(GL_FRONT,
1217                                         stencil->Function[0],
1218                                         stencil->Ref[0],
1219                                         stencil->ValueMask[0]);
1220               _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
1221               _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
1222                                       stencil->ZFailFunc[0],
1223                                       stencil->ZPassFunc[0]);
1224               /* back state */
1225               _mesa_StencilFuncSeparate(GL_BACK,
1226                                         stencil->Function[1],
1227                                         stencil->Ref[1],
1228                                         stencil->ValueMask[1]);
1229               _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
1230               _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
1231                                       stencil->ZFailFunc[1],
1232                                       stencil->ZPassFunc[1]);
1233            }
1234            break;
1235         case GL_TRANSFORM_BIT:
1236            {
1237               GLuint i;
1238               const struct gl_transform_attrib *xform;
1239               xform = (const struct gl_transform_attrib *) attr->data;
1240               _mesa_MatrixMode(xform->MatrixMode);
1241               if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
1242                  _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
1243
1244               /* restore clip planes */
1245               for (i = 0; i < MAX_CLIP_PLANES; i++) {
1246                  const GLuint mask = 1 << i;
1247                  const GLfloat *eyePlane = xform->EyeUserPlane[i];
1248                  COPY_4V(ctx->Transform.EyeUserPlane[i], eyePlane);
1249                  if (xform->ClipPlanesEnabled & mask) {
1250                     _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
1251                  }
1252                  else {
1253                     _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
1254                  }
1255                  if (ctx->Driver.ClipPlane)
1256                     ctx->Driver.ClipPlane( ctx, GL_CLIP_PLANE0 + i, eyePlane );
1257               }
1258
1259               /* normalize/rescale */
1260               if (xform->Normalize != ctx->Transform.Normalize)
1261                  _mesa_set_enable(ctx, GL_NORMALIZE,ctx->Transform.Normalize);
1262               if (xform->RescaleNormals != ctx->Transform.RescaleNormals)
1263                  _mesa_set_enable(ctx, GL_RESCALE_NORMAL_EXT,
1264                                   ctx->Transform.RescaleNormals);
1265               if (xform->DepthClamp != ctx->Transform.DepthClamp)
1266                  _mesa_set_enable(ctx, GL_DEPTH_CLAMP,
1267                                   ctx->Transform.DepthClamp);
1268            }
1269            break;
1270         case GL_TEXTURE_BIT:
1271            /* Take care of texture object reference counters */
1272            {
1273               struct texture_state *texstate
1274                  = (struct texture_state *) attr->data;
1275               pop_texture_group(ctx, texstate);
1276	       ctx->NewState |= _NEW_TEXTURE;
1277            }
1278            break;
1279         case GL_VIEWPORT_BIT:
1280            {
1281               const struct gl_viewport_attrib *vp;
1282               vp = (const struct gl_viewport_attrib *) attr->data;
1283               _mesa_Viewport(vp->X, vp->Y, vp->Width, vp->Height);
1284               _mesa_DepthRange(vp->Near, vp->Far);
1285            }
1286            break;
1287         case GL_MULTISAMPLE_BIT_ARB:
1288            {
1289               const struct gl_multisample_attrib *ms;
1290               ms = (const struct gl_multisample_attrib *) attr->data;
1291               _mesa_SampleCoverageARB(ms->SampleCoverageValue,
1292                                       ms->SampleCoverageInvert);
1293            }
1294            break;
1295
1296         default:
1297            _mesa_problem( ctx, "Bad attrib flag in PopAttrib");
1298            break;
1299      }
1300
1301      next = attr->next;
1302      FREE( attr->data );
1303      FREE( attr );
1304      attr = next;
1305   }
1306}
1307
1308
1309/**
1310 * Helper for incrementing/decrementing vertex buffer object reference
1311 * counts when pushing/popping the GL_CLIENT_VERTEX_ARRAY_BIT attribute group.
1312 */
1313static void
1314adjust_buffer_object_ref_counts(struct gl_array_object *arrayObj, GLint step)
1315{
1316   GLuint i;
1317
1318   arrayObj->Vertex.BufferObj->RefCount += step;
1319   arrayObj->Weight.BufferObj->RefCount += step;
1320   arrayObj->Normal.BufferObj->RefCount += step;
1321   arrayObj->Color.BufferObj->RefCount += step;
1322   arrayObj->SecondaryColor.BufferObj->RefCount += step;
1323   arrayObj->FogCoord.BufferObj->RefCount += step;
1324   arrayObj->Index.BufferObj->RefCount += step;
1325   arrayObj->EdgeFlag.BufferObj->RefCount += step;
1326   for (i = 0; i < Elements(arrayObj->TexCoord); i++)
1327      arrayObj->TexCoord[i].BufferObj->RefCount += step;
1328   for (i = 0; i < Elements(arrayObj->VertexAttrib); i++)
1329      arrayObj->VertexAttrib[i].BufferObj->RefCount += step;
1330}
1331
1332
1333/**
1334 * Copy gl_pixelstore_attrib from src to dst, updating buffer
1335 * object refcounts.
1336 */
1337static void
1338copy_pixelstore(GLcontext *ctx,
1339                struct gl_pixelstore_attrib *dst,
1340                const struct gl_pixelstore_attrib *src)
1341{
1342   dst->Alignment = src->Alignment;
1343   dst->RowLength = src->RowLength;
1344   dst->SkipPixels = src->SkipPixels;
1345   dst->SkipRows = src->SkipRows;
1346   dst->ImageHeight = src->ImageHeight;
1347   dst->SkipImages = src->SkipImages;
1348   dst->SwapBytes = src->SwapBytes;
1349   dst->LsbFirst = src->LsbFirst;
1350   dst->ClientStorage = src->ClientStorage;
1351   dst->Invert = src->Invert;
1352   _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
1353}
1354
1355
1356#define GL_CLIENT_PACK_BIT (1<<20)
1357#define GL_CLIENT_UNPACK_BIT (1<<21)
1358
1359
1360void GLAPIENTRY
1361_mesa_PushClientAttrib(GLbitfield mask)
1362{
1363   struct gl_attrib_node *head;
1364
1365   GET_CURRENT_CONTEXT(ctx);
1366   ASSERT_OUTSIDE_BEGIN_END(ctx);
1367
1368   if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
1369      _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushClientAttrib" );
1370      return;
1371   }
1372
1373   /* Build linked list of attribute nodes which save all attribute
1374    * groups specified by the mask.
1375    */
1376   head = NULL;
1377
1378   if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
1379      struct gl_pixelstore_attrib *attr;
1380      /* packing attribs */
1381      attr = CALLOC_STRUCT( gl_pixelstore_attrib );
1382      copy_pixelstore(ctx, attr, &ctx->Pack);
1383      save_attrib_data(&head, GL_CLIENT_PACK_BIT, attr);
1384      /* unpacking attribs */
1385      attr = CALLOC_STRUCT( gl_pixelstore_attrib );
1386      copy_pixelstore(ctx, attr, &ctx->Unpack);
1387      save_attrib_data(&head, GL_CLIENT_UNPACK_BIT, attr);
1388   }
1389
1390   if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1391      struct gl_array_attrib *attr;
1392      struct gl_array_object *obj;
1393
1394      attr = MALLOC_STRUCT( gl_array_attrib );
1395      obj = MALLOC_STRUCT( gl_array_object );
1396
1397#if FEATURE_ARB_vertex_buffer_object
1398      /* increment ref counts since we're copying pointers to these objects */
1399      ctx->Array.ArrayBufferObj->RefCount++;
1400      ctx->Array.ElementArrayBufferObj->RefCount++;
1401#endif
1402
1403      memcpy( attr, &ctx->Array, sizeof(struct gl_array_attrib) );
1404      memcpy( obj, ctx->Array.ArrayObj, sizeof(struct gl_array_object) );
1405
1406      attr->ArrayObj = obj;
1407
1408      save_attrib_data(&head, GL_CLIENT_VERTEX_ARRAY_BIT, attr);
1409
1410      /* bump reference counts on buffer objects */
1411      adjust_buffer_object_ref_counts(ctx->Array.ArrayObj, 1);
1412   }
1413
1414   ctx->ClientAttribStack[ctx->ClientAttribStackDepth] = head;
1415   ctx->ClientAttribStackDepth++;
1416}
1417
1418
1419
1420
1421void GLAPIENTRY
1422_mesa_PopClientAttrib(void)
1423{
1424   struct gl_attrib_node *node, *next;
1425
1426   GET_CURRENT_CONTEXT(ctx);
1427   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1428
1429   if (ctx->ClientAttribStackDepth == 0) {
1430      _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib" );
1431      return;
1432   }
1433
1434   ctx->ClientAttribStackDepth--;
1435   node = ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
1436
1437   while (node) {
1438      switch (node->kind) {
1439         case GL_CLIENT_PACK_BIT:
1440            {
1441               struct gl_pixelstore_attrib *store =
1442                  (struct gl_pixelstore_attrib *) node->data;
1443               copy_pixelstore(ctx, &ctx->Pack, store);
1444               _mesa_reference_buffer_object(ctx, &store->BufferObj, NULL);
1445            }
1446	    ctx->NewState |= _NEW_PACKUNPACK;
1447            break;
1448         case GL_CLIENT_UNPACK_BIT:
1449            {
1450               struct gl_pixelstore_attrib *store =
1451                  (struct gl_pixelstore_attrib *) node->data;
1452               copy_pixelstore(ctx, &ctx->Unpack, store);
1453               _mesa_reference_buffer_object(ctx, &store->BufferObj, NULL);
1454            }
1455	    ctx->NewState |= _NEW_PACKUNPACK;
1456            break;
1457         case GL_CLIENT_VERTEX_ARRAY_BIT: {
1458	    struct gl_array_attrib * data =
1459	      (struct gl_array_attrib *) node->data;
1460
1461            adjust_buffer_object_ref_counts(ctx->Array.ArrayObj, -1);
1462
1463            ctx->Array.ActiveTexture = data->ActiveTexture;
1464	    if (data->LockCount != 0)
1465	       _mesa_LockArraysEXT(data->LockFirst, data->LockCount);
1466	    else if (ctx->Array.LockCount)
1467	       _mesa_UnlockArraysEXT();
1468
1469	    _mesa_BindVertexArrayAPPLE( data->ArrayObj->Name );
1470
1471#if FEATURE_ARB_vertex_buffer_object
1472            _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB,
1473                                data->ArrayBufferObj->Name);
1474            _mesa_BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
1475                                data->ElementArrayBufferObj->Name);
1476#endif
1477
1478	    memcpy( ctx->Array.ArrayObj, data->ArrayObj,
1479		    sizeof( struct gl_array_object ) );
1480
1481	    FREE( data->ArrayObj );
1482
1483	    /* FIXME: Should some bits in ctx->Array->NewState also be set
1484	     * FIXME: here?  It seems like it should be set to inclusive-or
1485	     * FIXME: of the old ArrayObj->_Enabled and the new _Enabled.
1486	     */
1487
1488	    ctx->NewState |= _NEW_ARRAY;
1489            break;
1490	 }
1491         default:
1492            _mesa_problem( ctx, "Bad attrib flag in PopClientAttrib");
1493            break;
1494      }
1495
1496      next = node->next;
1497      FREE( node->data );
1498      FREE( node );
1499      node = next;
1500   }
1501}
1502
1503
1504void
1505_mesa_init_attrib_dispatch(struct _glapi_table *disp)
1506{
1507   SET_PopAttrib(disp, _mesa_PopAttrib);
1508   SET_PushAttrib(disp, _mesa_PushAttrib);
1509   SET_PopClientAttrib(disp, _mesa_PopClientAttrib);
1510   SET_PushClientAttrib(disp, _mesa_PushClientAttrib);
1511}
1512
1513
1514#endif /* FEATURE_attrib_stack */
1515
1516
1517/**
1518 * Free any attribute state data that might be attached to the context.
1519 */
1520void
1521_mesa_free_attrib_data(GLcontext *ctx)
1522{
1523   while (ctx->AttribStackDepth > 0) {
1524      struct gl_attrib_node *attr, *next;
1525
1526      ctx->AttribStackDepth--;
1527      attr = ctx->AttribStack[ctx->AttribStackDepth];
1528
1529      while (attr) {
1530         if (attr->kind == GL_TEXTURE_BIT) {
1531            struct texture_state *texstate = (struct texture_state*)attr->data;
1532            GLuint u, tgt;
1533            /* clear references to the saved texture objects */
1534            for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
1535               for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
1536                  _mesa_reference_texobj(&texstate->SavedTexRef[u][tgt], NULL);
1537               }
1538            }
1539         }
1540         else {
1541            /* any other chunks of state that requires special handling? */
1542         }
1543
1544         next = attr->next;
1545         free(attr->data);
1546         free(attr);
1547         attr = next;
1548      }
1549   }
1550}
1551
1552
1553void _mesa_init_attrib( GLcontext *ctx )
1554{
1555   /* Renderer and client attribute stacks */
1556   ctx->AttribStackDepth = 0;
1557   ctx->ClientAttribStackDepth = 0;
1558}
1559