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