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