attrib.c revision cdc920a0
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 "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 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      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   if (ctx->Color.BlendEnabled != enable->Blend) {
502      if (ctx->Extensions.EXT_draw_buffers2) {
503         GLuint i;
504         for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
505            _mesa_set_enablei(ctx, GL_BLEND, i, (enable->Blend >> i) & 1);
506         }
507      }
508      else {
509         _mesa_set_enable(ctx, GL_BLEND, (enable->Blend & 1));
510      }
511   }
512
513   for (i=0;i<MAX_CLIP_PLANES;i++) {
514      const GLuint mask = 1 << i;
515      if ((ctx->Transform.ClipPlanesEnabled & mask) != (enable->ClipPlanes & mask))
516	  _mesa_set_enable(ctx, (GLenum) (GL_CLIP_PLANE0 + i),
517			   (GLboolean) ((enable->ClipPlanes & mask) ? GL_TRUE : GL_FALSE));
518   }
519
520   TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled, enable->ColorMaterial,
521                   GL_COLOR_MATERIAL);
522   TEST_AND_UPDATE(ctx->Pixel.ColorTableEnabled[COLORTABLE_PRECONVOLUTION],
523                   enable->ColorTable[COLORTABLE_PRECONVOLUTION],
524                   GL_COLOR_TABLE);
525   TEST_AND_UPDATE(ctx->Pixel.ColorTableEnabled[COLORTABLE_POSTCONVOLUTION],
526                   enable->ColorTable[COLORTABLE_POSTCONVOLUTION],
527                   GL_POST_CONVOLUTION_COLOR_TABLE);
528   TEST_AND_UPDATE(ctx->Pixel.ColorTableEnabled[COLORTABLE_POSTCOLORMATRIX],
529                   enable->ColorTable[COLORTABLE_POSTCOLORMATRIX],
530                   GL_POST_COLOR_MATRIX_COLOR_TABLE);
531   TEST_AND_UPDATE(ctx->Polygon.CullFlag, enable->CullFace, GL_CULL_FACE);
532   TEST_AND_UPDATE(ctx->Transform.DepthClamp, enable->DepthClamp,
533		   GL_DEPTH_CLAMP);
534   TEST_AND_UPDATE(ctx->Depth.Test, enable->DepthTest, GL_DEPTH_TEST);
535   TEST_AND_UPDATE(ctx->Color.DitherFlag, enable->Dither, GL_DITHER);
536   TEST_AND_UPDATE(ctx->Pixel.Convolution1DEnabled, enable->Convolution1D,
537                   GL_CONVOLUTION_1D);
538   TEST_AND_UPDATE(ctx->Pixel.Convolution2DEnabled, enable->Convolution2D,
539                   GL_CONVOLUTION_2D);
540   TEST_AND_UPDATE(ctx->Pixel.Separable2DEnabled, enable->Separable2D,
541                   GL_SEPARABLE_2D);
542   TEST_AND_UPDATE(ctx->Fog.Enabled, enable->Fog, GL_FOG);
543   TEST_AND_UPDATE(ctx->Light.Enabled, enable->Lighting, GL_LIGHTING);
544   TEST_AND_UPDATE(ctx->Line.SmoothFlag, enable->LineSmooth, GL_LINE_SMOOTH);
545   TEST_AND_UPDATE(ctx->Line.StippleFlag, enable->LineStipple,
546                   GL_LINE_STIPPLE);
547   TEST_AND_UPDATE(ctx->Color.IndexLogicOpEnabled, enable->IndexLogicOp,
548                   GL_INDEX_LOGIC_OP);
549   TEST_AND_UPDATE(ctx->Color.ColorLogicOpEnabled, enable->ColorLogicOp,
550                   GL_COLOR_LOGIC_OP);
551
552   TEST_AND_UPDATE(ctx->Eval.Map1Color4, enable->Map1Color4, GL_MAP1_COLOR_4);
553   TEST_AND_UPDATE(ctx->Eval.Map1Index, enable->Map1Index, GL_MAP1_INDEX);
554   TEST_AND_UPDATE(ctx->Eval.Map1Normal, enable->Map1Normal, GL_MAP1_NORMAL);
555   TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord1, enable->Map1TextureCoord1,
556                   GL_MAP1_TEXTURE_COORD_1);
557   TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord2, enable->Map1TextureCoord2,
558                   GL_MAP1_TEXTURE_COORD_2);
559   TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord3, enable->Map1TextureCoord3,
560                   GL_MAP1_TEXTURE_COORD_3);
561   TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord4, enable->Map1TextureCoord4,
562                   GL_MAP1_TEXTURE_COORD_4);
563   TEST_AND_UPDATE(ctx->Eval.Map1Vertex3, enable->Map1Vertex3,
564                   GL_MAP1_VERTEX_3);
565   TEST_AND_UPDATE(ctx->Eval.Map1Vertex4, enable->Map1Vertex4,
566                   GL_MAP1_VERTEX_4);
567   for (i = 0; i < 16; i++) {
568      TEST_AND_UPDATE(ctx->Eval.Map1Attrib[i], enable->Map1Attrib[i],
569                      GL_MAP1_VERTEX_ATTRIB0_4_NV + i);
570   }
571
572   TEST_AND_UPDATE(ctx->Eval.Map2Color4, enable->Map2Color4, GL_MAP2_COLOR_4);
573   TEST_AND_UPDATE(ctx->Eval.Map2Index, enable->Map2Index, GL_MAP2_INDEX);
574   TEST_AND_UPDATE(ctx->Eval.Map2Normal, enable->Map2Normal, GL_MAP2_NORMAL);
575   TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord1, enable->Map2TextureCoord1,
576                   GL_MAP2_TEXTURE_COORD_1);
577   TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord2, enable->Map2TextureCoord2,
578                   GL_MAP2_TEXTURE_COORD_2);
579   TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord3, enable->Map2TextureCoord3,
580                   GL_MAP2_TEXTURE_COORD_3);
581   TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord4, enable->Map2TextureCoord4,
582                   GL_MAP2_TEXTURE_COORD_4);
583   TEST_AND_UPDATE(ctx->Eval.Map2Vertex3, enable->Map2Vertex3,
584                   GL_MAP2_VERTEX_3);
585   TEST_AND_UPDATE(ctx->Eval.Map2Vertex4, enable->Map2Vertex4,
586                   GL_MAP2_VERTEX_4);
587   for (i = 0; i < 16; i++) {
588      TEST_AND_UPDATE(ctx->Eval.Map2Attrib[i], enable->Map2Attrib[i],
589                      GL_MAP2_VERTEX_ATTRIB0_4_NV + i);
590   }
591
592   TEST_AND_UPDATE(ctx->Eval.AutoNormal, enable->AutoNormal, GL_AUTO_NORMAL);
593   TEST_AND_UPDATE(ctx->Transform.Normalize, enable->Normalize, GL_NORMALIZE);
594   TEST_AND_UPDATE(ctx->Transform.RescaleNormals, enable->RescaleNormals,
595                   GL_RESCALE_NORMAL_EXT);
596   TEST_AND_UPDATE(ctx->Transform.RasterPositionUnclipped,
597                   enable->RasterPositionUnclipped,
598                   GL_RASTER_POSITION_UNCLIPPED_IBM);
599   TEST_AND_UPDATE(ctx->Point.SmoothFlag, enable->PointSmooth,
600                   GL_POINT_SMOOTH);
601   if (ctx->Extensions.NV_point_sprite || ctx->Extensions.ARB_point_sprite) {
602      TEST_AND_UPDATE(ctx->Point.PointSprite, enable->PointSprite,
603                      GL_POINT_SPRITE_NV);
604   }
605   TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, enable->PolygonOffsetPoint,
606                   GL_POLYGON_OFFSET_POINT);
607   TEST_AND_UPDATE(ctx->Polygon.OffsetLine, enable->PolygonOffsetLine,
608                   GL_POLYGON_OFFSET_LINE);
609   TEST_AND_UPDATE(ctx->Polygon.OffsetFill, enable->PolygonOffsetFill,
610                   GL_POLYGON_OFFSET_FILL);
611   TEST_AND_UPDATE(ctx->Polygon.SmoothFlag, enable->PolygonSmooth,
612                   GL_POLYGON_SMOOTH);
613   TEST_AND_UPDATE(ctx->Polygon.StippleFlag, enable->PolygonStipple,
614                   GL_POLYGON_STIPPLE);
615   TEST_AND_UPDATE(ctx->Scissor.Enabled, enable->Scissor, GL_SCISSOR_TEST);
616   TEST_AND_UPDATE(ctx->Stencil.Enabled, enable->Stencil, GL_STENCIL_TEST);
617   if (ctx->Extensions.EXT_stencil_two_side) {
618      TEST_AND_UPDATE(ctx->Stencil.TestTwoSide, enable->StencilTwoSide, GL_STENCIL_TEST_TWO_SIDE_EXT);
619   }
620   TEST_AND_UPDATE(ctx->Multisample.Enabled, enable->MultisampleEnabled,
621                   GL_MULTISAMPLE_ARB);
622   TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
623                   enable->SampleAlphaToCoverage,
624                   GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
625   TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
626                   enable->SampleAlphaToOne,
627                   GL_SAMPLE_ALPHA_TO_ONE_ARB);
628   TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
629                   enable->SampleCoverage,
630                   GL_SAMPLE_COVERAGE_ARB);
631   TEST_AND_UPDATE(ctx->Multisample.SampleCoverageInvert,
632                   enable->SampleCoverageInvert,
633                   GL_SAMPLE_COVERAGE_INVERT_ARB);
634   /* GL_ARB_vertex_program, GL_NV_vertex_program */
635   TEST_AND_UPDATE(ctx->VertexProgram.Enabled,
636                   enable->VertexProgram,
637                   GL_VERTEX_PROGRAM_ARB);
638   TEST_AND_UPDATE(ctx->VertexProgram.PointSizeEnabled,
639                   enable->VertexProgramPointSize,
640                   GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
641   TEST_AND_UPDATE(ctx->VertexProgram.TwoSideEnabled,
642                   enable->VertexProgramTwoSide,
643                   GL_VERTEX_PROGRAM_TWO_SIDE_ARB);
644
645#undef TEST_AND_UPDATE
646
647   /* texture unit enables */
648   for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
649      const GLbitfield enabled = enable->Texture[i];
650      const GLbitfield genEnabled = enable->TexGen[i];
651
652      if (ctx->Texture.Unit[i].Enabled != enabled) {
653         _mesa_ActiveTextureARB(GL_TEXTURE0 + i);
654
655         _mesa_set_enable(ctx, GL_TEXTURE_1D,
656                          (enabled & TEXTURE_1D_BIT) ? GL_TRUE : GL_FALSE);
657         _mesa_set_enable(ctx, GL_TEXTURE_2D,
658                          (enabled & TEXTURE_2D_BIT) ? GL_TRUE : GL_FALSE);
659         _mesa_set_enable(ctx, GL_TEXTURE_3D,
660                          (enabled & TEXTURE_3D_BIT) ? GL_TRUE : GL_FALSE);
661         if (ctx->Extensions.NV_texture_rectangle) {
662            _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE_ARB,
663                             (enabled & TEXTURE_RECT_BIT) ? GL_TRUE : GL_FALSE);
664         }
665         if (ctx->Extensions.ARB_texture_cube_map) {
666            _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP,
667                             (enabled & TEXTURE_CUBE_BIT) ? GL_TRUE : GL_FALSE);
668         }
669         if (ctx->Extensions.MESA_texture_array) {
670            _mesa_set_enable(ctx, GL_TEXTURE_1D_ARRAY_EXT,
671                           (enabled & TEXTURE_1D_ARRAY_BIT) ? GL_TRUE : GL_FALSE);
672            _mesa_set_enable(ctx, GL_TEXTURE_2D_ARRAY_EXT,
673                           (enabled & TEXTURE_2D_ARRAY_BIT) ? GL_TRUE : GL_FALSE);
674         }
675      }
676
677      if (ctx->Texture.Unit[i].TexGenEnabled != genEnabled) {
678         _mesa_ActiveTextureARB(GL_TEXTURE0 + i);
679         _mesa_set_enable(ctx, GL_TEXTURE_GEN_S,
680                          (genEnabled & S_BIT) ? GL_TRUE : GL_FALSE);
681         _mesa_set_enable(ctx, GL_TEXTURE_GEN_T,
682                          (genEnabled & T_BIT) ? GL_TRUE : GL_FALSE);
683         _mesa_set_enable(ctx, GL_TEXTURE_GEN_R,
684                          (genEnabled & R_BIT) ? GL_TRUE : GL_FALSE);
685         _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q,
686                          (genEnabled & Q_BIT) ? GL_TRUE : GL_FALSE);
687      }
688
689      /* GL_SGI_texture_color_table */
690      ctx->Texture.Unit[i].ColorTableEnabled = enable->TextureColorTable[i];
691   }
692
693   _mesa_ActiveTextureARB(GL_TEXTURE0 + curTexUnitSave);
694}
695
696
697/**
698 * Pop/restore texture attribute/group state.
699 */
700static void
701pop_texture_group(GLcontext *ctx, struct texture_state *texstate)
702{
703   GLuint u;
704
705   _mesa_lock_context_textures(ctx);
706
707   for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
708      const struct gl_texture_unit *unit = &texstate->Texture.Unit[u];
709      GLuint tgt;
710
711      _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + u);
712      _mesa_set_enable(ctx, GL_TEXTURE_1D,
713                       (unit->Enabled & TEXTURE_1D_BIT) ? GL_TRUE : GL_FALSE);
714      _mesa_set_enable(ctx, GL_TEXTURE_2D,
715                       (unit->Enabled & TEXTURE_2D_BIT) ? GL_TRUE : GL_FALSE);
716      _mesa_set_enable(ctx, GL_TEXTURE_3D,
717                       (unit->Enabled & TEXTURE_3D_BIT) ? GL_TRUE : GL_FALSE);
718      if (ctx->Extensions.ARB_texture_cube_map) {
719         _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP_ARB,
720                     (unit->Enabled & TEXTURE_CUBE_BIT) ? GL_TRUE : GL_FALSE);
721      }
722      if (ctx->Extensions.NV_texture_rectangle) {
723         _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE_NV,
724                     (unit->Enabled & TEXTURE_RECT_BIT) ? GL_TRUE : GL_FALSE);
725      }
726      if (ctx->Extensions.MESA_texture_array) {
727         _mesa_set_enable(ctx, GL_TEXTURE_1D_ARRAY_EXT,
728                 (unit->Enabled & TEXTURE_1D_ARRAY_BIT) ? GL_TRUE : GL_FALSE);
729         _mesa_set_enable(ctx, GL_TEXTURE_2D_ARRAY_EXT,
730                 (unit->Enabled & TEXTURE_2D_ARRAY_BIT) ? GL_TRUE : GL_FALSE);
731      }
732
733      if (ctx->Extensions.SGI_texture_color_table) {
734         _mesa_set_enable(ctx, GL_TEXTURE_COLOR_TABLE_SGI,
735                          unit->ColorTableEnabled);
736      }
737      _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->EnvMode);
738      _mesa_TexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, unit->EnvColor);
739      _mesa_TexGeni(GL_S, GL_TEXTURE_GEN_MODE, unit->GenS.Mode);
740      _mesa_TexGeni(GL_T, GL_TEXTURE_GEN_MODE, unit->GenT.Mode);
741      _mesa_TexGeni(GL_R, GL_TEXTURE_GEN_MODE, unit->GenR.Mode);
742      _mesa_TexGeni(GL_Q, GL_TEXTURE_GEN_MODE, unit->GenQ.Mode);
743      _mesa_TexGenfv(GL_S, GL_OBJECT_PLANE, unit->GenS.ObjectPlane);
744      _mesa_TexGenfv(GL_T, GL_OBJECT_PLANE, unit->GenT.ObjectPlane);
745      _mesa_TexGenfv(GL_R, GL_OBJECT_PLANE, unit->GenR.ObjectPlane);
746      _mesa_TexGenfv(GL_Q, GL_OBJECT_PLANE, unit->GenQ.ObjectPlane);
747      /* Eye plane done differently to avoid re-transformation */
748      {
749         struct gl_texture_unit *destUnit = &ctx->Texture.Unit[u];
750         COPY_4FV(destUnit->GenS.EyePlane, unit->GenS.EyePlane);
751         COPY_4FV(destUnit->GenT.EyePlane, unit->GenT.EyePlane);
752         COPY_4FV(destUnit->GenR.EyePlane, unit->GenR.EyePlane);
753         COPY_4FV(destUnit->GenQ.EyePlane, unit->GenQ.EyePlane);
754         if (ctx->Driver.TexGen) {
755            ctx->Driver.TexGen(ctx, GL_S, GL_EYE_PLANE, unit->GenS.EyePlane);
756            ctx->Driver.TexGen(ctx, GL_T, GL_EYE_PLANE, unit->GenT.EyePlane);
757            ctx->Driver.TexGen(ctx, GL_R, GL_EYE_PLANE, unit->GenR.EyePlane);
758            ctx->Driver.TexGen(ctx, GL_Q, GL_EYE_PLANE, unit->GenQ.EyePlane);
759         }
760      }
761      _mesa_set_enable(ctx, GL_TEXTURE_GEN_S,
762                       ((unit->TexGenEnabled & S_BIT) ? GL_TRUE : GL_FALSE));
763      _mesa_set_enable(ctx, GL_TEXTURE_GEN_T,
764                       ((unit->TexGenEnabled & T_BIT) ? GL_TRUE : GL_FALSE));
765      _mesa_set_enable(ctx, GL_TEXTURE_GEN_R,
766                       ((unit->TexGenEnabled & R_BIT) ? GL_TRUE : GL_FALSE));
767      _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q,
768                       ((unit->TexGenEnabled & Q_BIT) ? GL_TRUE : GL_FALSE));
769      if (ctx->Extensions.EXT_texture_lod_bias) {
770         _mesa_TexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
771                       GL_TEXTURE_LOD_BIAS_EXT, unit->LodBias);
772      }
773      if (ctx->Extensions.EXT_texture_env_combine ||
774          ctx->Extensions.ARB_texture_env_combine) {
775         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,
776                       unit->Combine.ModeRGB);
777         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,
778                       unit->Combine.ModeA);
779         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB,
780                       unit->Combine.SourceRGB[0]);
781         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB,
782                       unit->Combine.SourceRGB[1]);
783         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB,
784                       unit->Combine.SourceRGB[2]);
785         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA,
786                       unit->Combine.SourceA[0]);
787         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA,
788                       unit->Combine.SourceA[1]);
789         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA,
790                       unit->Combine.SourceA[2]);
791         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB,
792                       unit->Combine.OperandRGB[0]);
793         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB,
794                       unit->Combine.OperandRGB[1]);
795         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB,
796                       unit->Combine.OperandRGB[2]);
797         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA,
798                       unit->Combine.OperandA[0]);
799         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA,
800                       unit->Combine.OperandA[1]);
801         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA,
802                       unit->Combine.OperandA[2]);
803         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE,
804                       1 << unit->Combine.ScaleShiftRGB);
805         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE,
806                       1 << unit->Combine.ScaleShiftA);
807      }
808
809      /* Restore texture object state for each target */
810      for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
811         const struct gl_texture_object *obj = NULL;
812         GLenum target;
813
814         obj = &texstate->SavedObj[u][tgt];
815
816         /* don't restore state for unsupported targets to prevent
817          * raising GL errors.
818          */
819         if (obj->Target == GL_TEXTURE_CUBE_MAP_ARB &&
820             !ctx->Extensions.ARB_texture_cube_map) {
821            continue;
822         }
823         else if (obj->Target == GL_TEXTURE_RECTANGLE_NV &&
824                  !ctx->Extensions.NV_texture_rectangle) {
825            continue;
826         }
827         else if ((obj->Target == GL_TEXTURE_1D_ARRAY_EXT ||
828                   obj->Target == GL_TEXTURE_2D_ARRAY_EXT) &&
829                  !ctx->Extensions.MESA_texture_array) {
830            continue;
831         }
832
833         target = obj->Target;
834
835         _mesa_BindTexture(target, obj->Name);
836
837         _mesa_TexParameterfv(target, GL_TEXTURE_BORDER_COLOR, obj->BorderColor.f);
838         _mesa_TexParameterf(target, GL_TEXTURE_PRIORITY, obj->Priority);
839         _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, obj->WrapS);
840         _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, obj->WrapT);
841         _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, obj->WrapR);
842         _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, obj->MinFilter);
843         _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, obj->MagFilter);
844         _mesa_TexParameterf(target, GL_TEXTURE_MIN_LOD, obj->MinLod);
845         _mesa_TexParameterf(target, GL_TEXTURE_MAX_LOD, obj->MaxLod);
846         _mesa_TexParameterf(target, GL_TEXTURE_LOD_BIAS, obj->LodBias);
847         _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, obj->BaseLevel);
848         if (target != GL_TEXTURE_RECTANGLE_ARB)
849            _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, obj->MaxLevel);
850         if (ctx->Extensions.EXT_texture_filter_anisotropic) {
851            _mesa_TexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
852                                obj->MaxAnisotropy);
853         }
854         if (ctx->Extensions.ARB_shadow_ambient) {
855            _mesa_TexParameterf(target, GL_TEXTURE_COMPARE_FAIL_VALUE_ARB,
856                                obj->CompareFailValue);
857         }
858      }
859
860      /* remove saved references to the texture objects */
861      for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
862         _mesa_reference_texobj(&texstate->SavedTexRef[u][tgt], NULL);
863      }
864   }
865
866   _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + texstate->Texture.CurrentUnit);
867
868   _mesa_unlock_context_textures(ctx);
869}
870
871
872/*
873 * This function is kind of long just because we have to call a lot
874 * of device driver functions to update device driver state.
875 *
876 * XXX As it is now, most of the pop-code calls immediate-mode Mesa functions
877 * in order to restore GL state.  This isn't terribly efficient but it
878 * ensures that dirty flags and any derived state gets updated correctly.
879 * We could at least check if the value to restore equals the current value
880 * and then skip the Mesa call.
881 */
882void GLAPIENTRY
883_mesa_PopAttrib(void)
884{
885   struct gl_attrib_node *attr, *next;
886   GET_CURRENT_CONTEXT(ctx);
887   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
888
889   if (ctx->AttribStackDepth == 0) {
890      _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopAttrib" );
891      return;
892   }
893
894   ctx->AttribStackDepth--;
895   attr = ctx->AttribStack[ctx->AttribStackDepth];
896
897   while (attr) {
898
899      if (MESA_VERBOSE & VERBOSE_API) {
900         _mesa_debug(ctx, "glPopAttrib %s\n",
901                     _mesa_lookup_enum_by_nr(attr->kind));
902      }
903
904      switch (attr->kind) {
905         case GL_ACCUM_BUFFER_BIT:
906            {
907               const struct gl_accum_attrib *accum;
908               accum = (const struct gl_accum_attrib *) attr->data;
909               _mesa_ClearAccum(accum->ClearColor[0],
910                                accum->ClearColor[1],
911                                accum->ClearColor[2],
912                                accum->ClearColor[3]);
913            }
914            break;
915         case GL_COLOR_BUFFER_BIT:
916            {
917               const struct gl_colorbuffer_attrib *color;
918
919               color = (const struct gl_colorbuffer_attrib *) attr->data;
920               _mesa_ClearIndex((GLfloat) color->ClearIndex);
921               _mesa_ClearColor(color->ClearColor[0],
922                                color->ClearColor[1],
923                                color->ClearColor[2],
924                                color->ClearColor[3]);
925               _mesa_IndexMask(color->IndexMask);
926               if (!ctx->Extensions.EXT_draw_buffers2) {
927                  _mesa_ColorMask((GLboolean) (color->ColorMask[0][0] != 0),
928                                  (GLboolean) (color->ColorMask[0][1] != 0),
929                                  (GLboolean) (color->ColorMask[0][2] != 0),
930                                  (GLboolean) (color->ColorMask[0][3] != 0));
931               }
932               else {
933                  GLuint i;
934                  for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
935                     _mesa_ColorMaskIndexed(i,
936                                  (GLboolean) (color->ColorMask[i][0] != 0),
937                                  (GLboolean) (color->ColorMask[i][1] != 0),
938                                  (GLboolean) (color->ColorMask[i][2] != 0),
939                                  (GLboolean) (color->ColorMask[i][3] != 0));
940                  }
941               }
942               {
943                  /* Need to determine if more than one color output is
944                   * specified.  If so, call glDrawBuffersARB, else call
945                   * glDrawBuffer().  This is a subtle, but essential point
946                   * since GL_FRONT (for example) is illegal for the former
947                   * function, but legal for the later.
948                   */
949                  GLboolean multipleBuffers = GL_FALSE;
950		  GLuint i;
951
952		  for (i = 1; i < ctx->Const.MaxDrawBuffers; i++) {
953		     if (color->DrawBuffer[i] != GL_NONE) {
954			multipleBuffers = GL_TRUE;
955			break;
956		     }
957                  }
958                  /* Call the API_level functions, not _mesa_drawbuffers()
959                   * since we need to do error checking on the pop'd
960                   * GL_DRAW_BUFFER.
961                   * Ex: if GL_FRONT were pushed, but we're popping with a
962                   * user FBO bound, GL_FRONT will be illegal and we'll need
963                   * to record that error.  Per OpenGL ARB decision.
964                   */
965                  if (multipleBuffers)
966                     _mesa_DrawBuffersARB(ctx->Const.MaxDrawBuffers,
967                                          color->DrawBuffer);
968                  else
969                     _mesa_DrawBuffer(color->DrawBuffer[0]);
970               }
971               _mesa_set_enable(ctx, GL_ALPHA_TEST, color->AlphaEnabled);
972               _mesa_AlphaFunc(color->AlphaFunc, color->AlphaRef);
973               if (ctx->Color.BlendEnabled != color->BlendEnabled) {
974                  if (ctx->Extensions.EXT_draw_buffers2) {
975                     GLuint i;
976                     for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
977                        _mesa_set_enablei(ctx, GL_BLEND, i,
978                                          (color->BlendEnabled >> i) & 1);
979                     }
980                  }
981                  else {
982                     _mesa_set_enable(ctx, GL_BLEND, (color->BlendEnabled & 1));
983                  }
984               }
985               _mesa_BlendFuncSeparateEXT(color->BlendSrcRGB,
986                                          color->BlendDstRGB,
987                                          color->BlendSrcA,
988                                          color->BlendDstA);
989	       /* This special case is because glBlendEquationSeparateEXT
990		* cannot take GL_LOGIC_OP as a parameter.
991		*/
992	       if ( color->BlendEquationRGB == color->BlendEquationA ) {
993		  _mesa_BlendEquation(color->BlendEquationRGB);
994	       }
995	       else {
996		  _mesa_BlendEquationSeparateEXT(color->BlendEquationRGB,
997						 color->BlendEquationA);
998	       }
999               _mesa_BlendColor(color->BlendColor[0],
1000                                color->BlendColor[1],
1001                                color->BlendColor[2],
1002                                color->BlendColor[3]);
1003               _mesa_LogicOp(color->LogicOp);
1004               _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP,
1005                                color->ColorLogicOpEnabled);
1006               _mesa_set_enable(ctx, GL_INDEX_LOGIC_OP,
1007                                color->IndexLogicOpEnabled);
1008               _mesa_set_enable(ctx, GL_DITHER, color->DitherFlag);
1009            }
1010            break;
1011         case GL_CURRENT_BIT:
1012	    FLUSH_CURRENT( ctx, 0 );
1013            memcpy( &ctx->Current, attr->data,
1014		    sizeof(struct gl_current_attrib) );
1015            break;
1016         case GL_DEPTH_BUFFER_BIT:
1017            {
1018               const struct gl_depthbuffer_attrib *depth;
1019               depth = (const struct gl_depthbuffer_attrib *) attr->data;
1020               _mesa_DepthFunc(depth->Func);
1021               _mesa_ClearDepth(depth->Clear);
1022               _mesa_set_enable(ctx, GL_DEPTH_TEST, depth->Test);
1023               _mesa_DepthMask(depth->Mask);
1024            }
1025            break;
1026         case GL_ENABLE_BIT:
1027            {
1028               const struct gl_enable_attrib *enable;
1029               enable = (const struct gl_enable_attrib *) attr->data;
1030               pop_enable_group(ctx, enable);
1031	       ctx->NewState |= _NEW_ALL;
1032            }
1033            break;
1034         case GL_EVAL_BIT:
1035            memcpy( &ctx->Eval, attr->data, sizeof(struct gl_eval_attrib) );
1036	    ctx->NewState |= _NEW_EVAL;
1037            break;
1038         case GL_FOG_BIT:
1039            {
1040               const struct gl_fog_attrib *fog;
1041               fog = (const struct gl_fog_attrib *) attr->data;
1042               _mesa_set_enable(ctx, GL_FOG, fog->Enabled);
1043               _mesa_Fogfv(GL_FOG_COLOR, fog->Color);
1044               _mesa_Fogf(GL_FOG_DENSITY, fog->Density);
1045               _mesa_Fogf(GL_FOG_START, fog->Start);
1046               _mesa_Fogf(GL_FOG_END, fog->End);
1047               _mesa_Fogf(GL_FOG_INDEX, fog->Index);
1048               _mesa_Fogi(GL_FOG_MODE, fog->Mode);
1049            }
1050            break;
1051         case GL_HINT_BIT:
1052            {
1053               const struct gl_hint_attrib *hint;
1054               hint = (const struct gl_hint_attrib *) attr->data;
1055               _mesa_Hint(GL_PERSPECTIVE_CORRECTION_HINT,
1056                          hint->PerspectiveCorrection );
1057               _mesa_Hint(GL_POINT_SMOOTH_HINT, hint->PointSmooth);
1058               _mesa_Hint(GL_LINE_SMOOTH_HINT, hint->LineSmooth);
1059               _mesa_Hint(GL_POLYGON_SMOOTH_HINT, hint->PolygonSmooth);
1060               _mesa_Hint(GL_FOG_HINT, hint->Fog);
1061               _mesa_Hint(GL_CLIP_VOLUME_CLIPPING_HINT_EXT,
1062                          hint->ClipVolumeClipping);
1063	       _mesa_Hint(GL_TEXTURE_COMPRESSION_HINT_ARB,
1064			  hint->TextureCompression);
1065            }
1066            break;
1067         case GL_LIGHTING_BIT:
1068            {
1069               GLuint i;
1070               const struct gl_light_attrib *light;
1071               light = (const struct gl_light_attrib *) attr->data;
1072               /* lighting enable */
1073               _mesa_set_enable(ctx, GL_LIGHTING, light->Enabled);
1074               /* per-light state */
1075               if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
1076                  _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
1077
1078               for (i = 0; i < ctx->Const.MaxLights; i++) {
1079                  const struct gl_light *l = &light->Light[i];
1080                  _mesa_set_enable(ctx, GL_LIGHT0 + i, l->Enabled);
1081                  _mesa_light(ctx, i, GL_AMBIENT, l->Ambient);
1082                  _mesa_light(ctx, i, GL_DIFFUSE, l->Diffuse);
1083                  _mesa_light(ctx, i, GL_SPECULAR, l->Specular );
1084                  _mesa_light(ctx, i, GL_POSITION, l->EyePosition);
1085                  _mesa_light(ctx, i, GL_SPOT_DIRECTION, l->SpotDirection);
1086                  {
1087                     GLfloat p[4] = { 0 };
1088                     p[0] = l->SpotExponent;
1089                     _mesa_light(ctx, i, GL_SPOT_EXPONENT, p);
1090                  }
1091                  {
1092                     GLfloat p[4] = { 0 };
1093                     p[0] = l->SpotCutoff;
1094                     _mesa_light(ctx, i, GL_SPOT_CUTOFF, p);
1095                  }
1096                  {
1097                     GLfloat p[4] = { 0 };
1098                     p[0] = l->ConstantAttenuation;
1099                     _mesa_light(ctx, i, GL_CONSTANT_ATTENUATION, p);
1100                  }
1101                  {
1102                     GLfloat p[4] = { 0 };
1103                     p[0] = l->LinearAttenuation;
1104                     _mesa_light(ctx, i, GL_LINEAR_ATTENUATION, p);
1105                  }
1106                  {
1107                     GLfloat p[4] = { 0 };
1108                     p[0] = l->QuadraticAttenuation;
1109                     _mesa_light(ctx, i, GL_QUADRATIC_ATTENUATION, p);
1110                  }
1111                }
1112               /* light model */
1113               _mesa_LightModelfv(GL_LIGHT_MODEL_AMBIENT,
1114                                  light->Model.Ambient);
1115               _mesa_LightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,
1116                                 (GLfloat) light->Model.LocalViewer);
1117               _mesa_LightModelf(GL_LIGHT_MODEL_TWO_SIDE,
1118                                 (GLfloat) light->Model.TwoSide);
1119               _mesa_LightModelf(GL_LIGHT_MODEL_COLOR_CONTROL,
1120                                 (GLfloat) light->Model.ColorControl);
1121               /* shade model */
1122               _mesa_ShadeModel(light->ShadeModel);
1123               /* color material */
1124               _mesa_ColorMaterial(light->ColorMaterialFace,
1125                                   light->ColorMaterialMode);
1126               _mesa_set_enable(ctx, GL_COLOR_MATERIAL,
1127                                light->ColorMaterialEnabled);
1128               /* materials */
1129               memcpy(&ctx->Light.Material, &light->Material,
1130                      sizeof(struct gl_material));
1131            }
1132            break;
1133         case GL_LINE_BIT:
1134            {
1135               const struct gl_line_attrib *line;
1136               line = (const struct gl_line_attrib *) attr->data;
1137               _mesa_set_enable(ctx, GL_LINE_SMOOTH, line->SmoothFlag);
1138               _mesa_set_enable(ctx, GL_LINE_STIPPLE, line->StippleFlag);
1139               _mesa_LineStipple(line->StippleFactor, line->StipplePattern);
1140               _mesa_LineWidth(line->Width);
1141            }
1142            break;
1143         case GL_LIST_BIT:
1144            memcpy( &ctx->List, attr->data, sizeof(struct gl_list_attrib) );
1145            break;
1146         case GL_PIXEL_MODE_BIT:
1147            memcpy( &ctx->Pixel, attr->data, sizeof(struct gl_pixel_attrib) );
1148            /* XXX what other pixel state needs to be set by function calls? */
1149            _mesa_ReadBuffer(ctx->Pixel.ReadBuffer);
1150	    ctx->NewState |= _NEW_PIXEL;
1151            break;
1152         case GL_POINT_BIT:
1153            {
1154               const struct gl_point_attrib *point;
1155               point = (const struct gl_point_attrib *) attr->data;
1156               _mesa_PointSize(point->Size);
1157               _mesa_set_enable(ctx, GL_POINT_SMOOTH, point->SmoothFlag);
1158               if (ctx->Extensions.EXT_point_parameters) {
1159                  _mesa_PointParameterfv(GL_DISTANCE_ATTENUATION_EXT,
1160                                         point->Params);
1161                  _mesa_PointParameterf(GL_POINT_SIZE_MIN_EXT,
1162                                        point->MinSize);
1163                  _mesa_PointParameterf(GL_POINT_SIZE_MAX_EXT,
1164                                        point->MaxSize);
1165                  _mesa_PointParameterf(GL_POINT_FADE_THRESHOLD_SIZE_EXT,
1166                                        point->Threshold);
1167               }
1168               if (ctx->Extensions.NV_point_sprite
1169		   || ctx->Extensions.ARB_point_sprite) {
1170                  GLuint u;
1171                  for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
1172                     _mesa_TexEnvi(GL_POINT_SPRITE_NV, GL_COORD_REPLACE_NV,
1173                                   (GLint) point->CoordReplace[u]);
1174                  }
1175                  _mesa_set_enable(ctx, GL_POINT_SPRITE_NV,point->PointSprite);
1176                  if (ctx->Extensions.NV_point_sprite)
1177                     _mesa_PointParameteri(GL_POINT_SPRITE_R_MODE_NV,
1178                                           ctx->Point.SpriteRMode);
1179                  _mesa_PointParameterf(GL_POINT_SPRITE_COORD_ORIGIN,
1180                                        (GLfloat)ctx->Point.SpriteOrigin);
1181               }
1182            }
1183            break;
1184         case GL_POLYGON_BIT:
1185            {
1186               const struct gl_polygon_attrib *polygon;
1187               polygon = (const struct gl_polygon_attrib *) attr->data;
1188               _mesa_CullFace(polygon->CullFaceMode);
1189               _mesa_FrontFace(polygon->FrontFace);
1190               _mesa_PolygonMode(GL_FRONT, polygon->FrontMode);
1191               _mesa_PolygonMode(GL_BACK, polygon->BackMode);
1192               _mesa_PolygonOffset(polygon->OffsetFactor,
1193                                   polygon->OffsetUnits);
1194               _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, polygon->SmoothFlag);
1195               _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, polygon->StippleFlag);
1196               _mesa_set_enable(ctx, GL_CULL_FACE, polygon->CullFlag);
1197               _mesa_set_enable(ctx, GL_POLYGON_OFFSET_POINT,
1198                                polygon->OffsetPoint);
1199               _mesa_set_enable(ctx, GL_POLYGON_OFFSET_LINE,
1200                                polygon->OffsetLine);
1201               _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL,
1202                                polygon->OffsetFill);
1203            }
1204            break;
1205	 case GL_POLYGON_STIPPLE_BIT:
1206	    memcpy( ctx->PolygonStipple, attr->data, 32*sizeof(GLuint) );
1207	    ctx->NewState |= _NEW_POLYGONSTIPPLE;
1208	    if (ctx->Driver.PolygonStipple)
1209	       ctx->Driver.PolygonStipple( ctx, (const GLubyte *) attr->data );
1210	    break;
1211         case GL_SCISSOR_BIT:
1212            {
1213               const struct gl_scissor_attrib *scissor;
1214               scissor = (const struct gl_scissor_attrib *) attr->data;
1215               _mesa_Scissor(scissor->X, scissor->Y,
1216                             scissor->Width, scissor->Height);
1217               _mesa_set_enable(ctx, GL_SCISSOR_TEST, scissor->Enabled);
1218            }
1219            break;
1220         case GL_STENCIL_BUFFER_BIT:
1221            {
1222               const struct gl_stencil_attrib *stencil;
1223               stencil = (const struct gl_stencil_attrib *) attr->data;
1224               _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
1225               _mesa_ClearStencil(stencil->Clear);
1226               if (ctx->Extensions.EXT_stencil_two_side) {
1227                  _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
1228                                   stencil->TestTwoSide);
1229                  _mesa_ActiveStencilFaceEXT(stencil->ActiveFace
1230                                             ? GL_BACK : GL_FRONT);
1231               }
1232               /* front state */
1233               _mesa_StencilFuncSeparate(GL_FRONT,
1234                                         stencil->Function[0],
1235                                         stencil->Ref[0],
1236                                         stencil->ValueMask[0]);
1237               _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
1238               _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
1239                                       stencil->ZFailFunc[0],
1240                                       stencil->ZPassFunc[0]);
1241               /* back state */
1242               _mesa_StencilFuncSeparate(GL_BACK,
1243                                         stencil->Function[1],
1244                                         stencil->Ref[1],
1245                                         stencil->ValueMask[1]);
1246               _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
1247               _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
1248                                       stencil->ZFailFunc[1],
1249                                       stencil->ZPassFunc[1]);
1250            }
1251            break;
1252         case GL_TRANSFORM_BIT:
1253            {
1254               GLuint i;
1255               const struct gl_transform_attrib *xform;
1256               xform = (const struct gl_transform_attrib *) attr->data;
1257               _mesa_MatrixMode(xform->MatrixMode);
1258               if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
1259                  _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
1260
1261               /* restore clip planes */
1262               for (i = 0; i < MAX_CLIP_PLANES; i++) {
1263                  const GLuint mask = 1 << i;
1264                  const GLfloat *eyePlane = xform->EyeUserPlane[i];
1265                  COPY_4V(ctx->Transform.EyeUserPlane[i], eyePlane);
1266                  if (xform->ClipPlanesEnabled & mask) {
1267                     _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
1268                  }
1269                  else {
1270                     _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
1271                  }
1272                  if (ctx->Driver.ClipPlane)
1273                     ctx->Driver.ClipPlane( ctx, GL_CLIP_PLANE0 + i, eyePlane );
1274               }
1275
1276               /* normalize/rescale */
1277               if (xform->Normalize != ctx->Transform.Normalize)
1278                  _mesa_set_enable(ctx, GL_NORMALIZE,ctx->Transform.Normalize);
1279               if (xform->RescaleNormals != ctx->Transform.RescaleNormals)
1280                  _mesa_set_enable(ctx, GL_RESCALE_NORMAL_EXT,
1281                                   ctx->Transform.RescaleNormals);
1282               if (xform->DepthClamp != ctx->Transform.DepthClamp)
1283                  _mesa_set_enable(ctx, GL_DEPTH_CLAMP,
1284                                   ctx->Transform.DepthClamp);
1285            }
1286            break;
1287         case GL_TEXTURE_BIT:
1288            /* Take care of texture object reference counters */
1289            {
1290               struct texture_state *texstate
1291                  = (struct texture_state *) attr->data;
1292               pop_texture_group(ctx, texstate);
1293	       ctx->NewState |= _NEW_TEXTURE;
1294            }
1295            break;
1296         case GL_VIEWPORT_BIT:
1297            {
1298               const struct gl_viewport_attrib *vp;
1299               vp = (const struct gl_viewport_attrib *) attr->data;
1300               _mesa_Viewport(vp->X, vp->Y, vp->Width, vp->Height);
1301               _mesa_DepthRange(vp->Near, vp->Far);
1302            }
1303            break;
1304         case GL_MULTISAMPLE_BIT_ARB:
1305            {
1306               const struct gl_multisample_attrib *ms;
1307               ms = (const struct gl_multisample_attrib *) attr->data;
1308               _mesa_SampleCoverageARB(ms->SampleCoverageValue,
1309                                       ms->SampleCoverageInvert);
1310            }
1311            break;
1312
1313         default:
1314            _mesa_problem( ctx, "Bad attrib flag in PopAttrib");
1315            break;
1316      }
1317
1318      next = attr->next;
1319      FREE( attr->data );
1320      FREE( attr );
1321      attr = next;
1322   }
1323}
1324
1325
1326/**
1327 * Helper for incrementing/decrementing vertex buffer object reference
1328 * counts when pushing/popping the GL_CLIENT_VERTEX_ARRAY_BIT attribute group.
1329 */
1330static void
1331adjust_buffer_object_ref_counts(struct gl_array_object *arrayObj, GLint step)
1332{
1333   GLuint i;
1334
1335   arrayObj->Vertex.BufferObj->RefCount += step;
1336   arrayObj->Weight.BufferObj->RefCount += step;
1337   arrayObj->Normal.BufferObj->RefCount += step;
1338   arrayObj->Color.BufferObj->RefCount += step;
1339   arrayObj->SecondaryColor.BufferObj->RefCount += step;
1340   arrayObj->FogCoord.BufferObj->RefCount += step;
1341   arrayObj->Index.BufferObj->RefCount += step;
1342   arrayObj->EdgeFlag.BufferObj->RefCount += step;
1343   for (i = 0; i < Elements(arrayObj->TexCoord); i++)
1344      arrayObj->TexCoord[i].BufferObj->RefCount += step;
1345   for (i = 0; i < Elements(arrayObj->VertexAttrib); i++)
1346      arrayObj->VertexAttrib[i].BufferObj->RefCount += step;
1347}
1348
1349
1350/**
1351 * Copy gl_pixelstore_attrib from src to dst, updating buffer
1352 * object refcounts.
1353 */
1354static void
1355copy_pixelstore(GLcontext *ctx,
1356                struct gl_pixelstore_attrib *dst,
1357                const struct gl_pixelstore_attrib *src)
1358{
1359   dst->Alignment = src->Alignment;
1360   dst->RowLength = src->RowLength;
1361   dst->SkipPixels = src->SkipPixels;
1362   dst->SkipRows = src->SkipRows;
1363   dst->ImageHeight = src->ImageHeight;
1364   dst->SkipImages = src->SkipImages;
1365   dst->SwapBytes = src->SwapBytes;
1366   dst->LsbFirst = src->LsbFirst;
1367   dst->ClientStorage = src->ClientStorage;
1368   dst->Invert = src->Invert;
1369   _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
1370}
1371
1372
1373#define GL_CLIENT_PACK_BIT (1<<20)
1374#define GL_CLIENT_UNPACK_BIT (1<<21)
1375
1376
1377void GLAPIENTRY
1378_mesa_PushClientAttrib(GLbitfield mask)
1379{
1380   struct gl_attrib_node *head;
1381
1382   GET_CURRENT_CONTEXT(ctx);
1383   ASSERT_OUTSIDE_BEGIN_END(ctx);
1384
1385   if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
1386      _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushClientAttrib" );
1387      return;
1388   }
1389
1390   /* Build linked list of attribute nodes which save all attribute
1391    * groups specified by the mask.
1392    */
1393   head = NULL;
1394
1395   if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
1396      struct gl_pixelstore_attrib *attr;
1397      /* packing attribs */
1398      attr = CALLOC_STRUCT( gl_pixelstore_attrib );
1399      copy_pixelstore(ctx, attr, &ctx->Pack);
1400      save_attrib_data(&head, GL_CLIENT_PACK_BIT, attr);
1401      /* unpacking attribs */
1402      attr = CALLOC_STRUCT( gl_pixelstore_attrib );
1403      copy_pixelstore(ctx, attr, &ctx->Unpack);
1404      save_attrib_data(&head, GL_CLIENT_UNPACK_BIT, attr);
1405   }
1406
1407   if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1408      struct gl_array_attrib *attr;
1409      struct gl_array_object *obj;
1410
1411      attr = MALLOC_STRUCT( gl_array_attrib );
1412      obj = MALLOC_STRUCT( gl_array_object );
1413
1414#if FEATURE_ARB_vertex_buffer_object
1415      /* increment ref counts since we're copying pointers to these objects */
1416      ctx->Array.ArrayBufferObj->RefCount++;
1417      ctx->Array.ElementArrayBufferObj->RefCount++;
1418#endif
1419
1420      memcpy( attr, &ctx->Array, sizeof(struct gl_array_attrib) );
1421      memcpy( obj, ctx->Array.ArrayObj, sizeof(struct gl_array_object) );
1422
1423      attr->ArrayObj = obj;
1424
1425      save_attrib_data(&head, GL_CLIENT_VERTEX_ARRAY_BIT, attr);
1426
1427      /* bump reference counts on buffer objects */
1428      adjust_buffer_object_ref_counts(ctx->Array.ArrayObj, 1);
1429   }
1430
1431   ctx->ClientAttribStack[ctx->ClientAttribStackDepth] = head;
1432   ctx->ClientAttribStackDepth++;
1433}
1434
1435
1436
1437
1438void GLAPIENTRY
1439_mesa_PopClientAttrib(void)
1440{
1441   struct gl_attrib_node *node, *next;
1442
1443   GET_CURRENT_CONTEXT(ctx);
1444   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1445
1446   if (ctx->ClientAttribStackDepth == 0) {
1447      _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib" );
1448      return;
1449   }
1450
1451   ctx->ClientAttribStackDepth--;
1452   node = ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
1453
1454   while (node) {
1455      switch (node->kind) {
1456         case GL_CLIENT_PACK_BIT:
1457            {
1458               struct gl_pixelstore_attrib *store =
1459                  (struct gl_pixelstore_attrib *) node->data;
1460               copy_pixelstore(ctx, &ctx->Pack, store);
1461               _mesa_reference_buffer_object(ctx, &store->BufferObj, NULL);
1462            }
1463	    ctx->NewState |= _NEW_PACKUNPACK;
1464            break;
1465         case GL_CLIENT_UNPACK_BIT:
1466            {
1467               struct gl_pixelstore_attrib *store =
1468                  (struct gl_pixelstore_attrib *) node->data;
1469               copy_pixelstore(ctx, &ctx->Unpack, store);
1470               _mesa_reference_buffer_object(ctx, &store->BufferObj, NULL);
1471            }
1472	    ctx->NewState |= _NEW_PACKUNPACK;
1473            break;
1474         case GL_CLIENT_VERTEX_ARRAY_BIT: {
1475	    struct gl_array_attrib * data =
1476	      (struct gl_array_attrib *) node->data;
1477
1478            adjust_buffer_object_ref_counts(ctx->Array.ArrayObj, -1);
1479
1480            ctx->Array.ActiveTexture = data->ActiveTexture;
1481	    if (data->LockCount != 0)
1482	       _mesa_LockArraysEXT(data->LockFirst, data->LockCount);
1483	    else if (ctx->Array.LockCount)
1484	       _mesa_UnlockArraysEXT();
1485
1486	    _mesa_BindVertexArrayAPPLE( data->ArrayObj->Name );
1487
1488#if FEATURE_ARB_vertex_buffer_object
1489            _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB,
1490                                data->ArrayBufferObj->Name);
1491            _mesa_BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
1492                                data->ElementArrayBufferObj->Name);
1493#endif
1494
1495	    memcpy( ctx->Array.ArrayObj, data->ArrayObj,
1496		    sizeof( struct gl_array_object ) );
1497
1498	    FREE( data->ArrayObj );
1499
1500	    /* FIXME: Should some bits in ctx->Array->NewState also be set
1501	     * FIXME: here?  It seems like it should be set to inclusive-or
1502	     * FIXME: of the old ArrayObj->_Enabled and the new _Enabled.
1503	     */
1504
1505	    ctx->NewState |= _NEW_ARRAY;
1506            break;
1507	 }
1508         default:
1509            _mesa_problem( ctx, "Bad attrib flag in PopClientAttrib");
1510            break;
1511      }
1512
1513      next = node->next;
1514      FREE( node->data );
1515      FREE( node );
1516      node = next;
1517   }
1518}
1519
1520
1521void
1522_mesa_init_attrib_dispatch(struct _glapi_table *disp)
1523{
1524   SET_PopAttrib(disp, _mesa_PopAttrib);
1525   SET_PushAttrib(disp, _mesa_PushAttrib);
1526   SET_PopClientAttrib(disp, _mesa_PopClientAttrib);
1527   SET_PushClientAttrib(disp, _mesa_PushClientAttrib);
1528}
1529
1530
1531#endif /* FEATURE_attrib_stack */
1532
1533
1534/**
1535 * Free any attribute state data that might be attached to the context.
1536 */
1537void
1538_mesa_free_attrib_data(GLcontext *ctx)
1539{
1540   while (ctx->AttribStackDepth > 0) {
1541      struct gl_attrib_node *attr, *next;
1542
1543      ctx->AttribStackDepth--;
1544      attr = ctx->AttribStack[ctx->AttribStackDepth];
1545
1546      while (attr) {
1547         if (attr->kind == GL_TEXTURE_BIT) {
1548            struct texture_state *texstate = (struct texture_state*)attr->data;
1549            GLuint u, tgt;
1550            /* clear references to the saved texture objects */
1551            for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
1552               for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
1553                  _mesa_reference_texobj(&texstate->SavedTexRef[u][tgt], NULL);
1554               }
1555            }
1556         }
1557         else {
1558            /* any other chunks of state that requires special handling? */
1559         }
1560
1561         next = attr->next;
1562         free(attr->data);
1563         free(attr);
1564         attr = next;
1565      }
1566   }
1567}
1568
1569
1570void _mesa_init_attrib( GLcontext *ctx )
1571{
1572   /* Renderer and client attribute stacks */
1573   ctx->AttribStackDepth = 0;
1574   ctx->ClientAttribStackDepth = 0;
1575}
1576