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