OSDN Git Service

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