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