OSDN Git Service

mesa/main: Make FEATURE_attrib_stack follow feature conventions.
[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  * 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 "colortab.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 "matrix.h"
46 #include "multisample.h"
47 #include "points.h"
48 #include "polygon.h"
49 #include "scissor.h"
50 #include "simple_list.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 "glapi/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 ColorTable[COLORTABLE_MAX];
74    GLboolean Convolution1D;
75    GLboolean Convolution2D;
76    GLboolean Separable2D;
77    GLboolean CullFace;
78    GLboolean DepthClamp;
79    GLboolean DepthTest;
80    GLboolean Dither;
81    GLboolean Fog;
82    GLboolean Histogram;
83    GLboolean Light[MAX_LIGHTS];
84    GLboolean Lighting;
85    GLboolean LineSmooth;
86    GLboolean LineStipple;
87    GLboolean IndexLogicOp;
88    GLboolean ColorLogicOp;
89
90    GLboolean Map1Color4;
91    GLboolean Map1Index;
92    GLboolean Map1Normal;
93    GLboolean Map1TextureCoord1;
94    GLboolean Map1TextureCoord2;
95    GLboolean Map1TextureCoord3;
96    GLboolean Map1TextureCoord4;
97    GLboolean Map1Vertex3;
98    GLboolean Map1Vertex4;
99    GLboolean Map1Attrib[16];  /* GL_NV_vertex_program */
100    GLboolean Map2Color4;
101    GLboolean Map2Index;
102    GLboolean Map2Normal;
103    GLboolean Map2TextureCoord1;
104    GLboolean Map2TextureCoord2;
105    GLboolean Map2TextureCoord3;
106    GLboolean Map2TextureCoord4;
107    GLboolean Map2Vertex3;
108    GLboolean Map2Vertex4;
109    GLboolean Map2Attrib[16];  /* GL_NV_vertex_program */
110
111    GLboolean MinMax;
112    GLboolean Normalize;
113    GLboolean PixelTexture;
114    GLboolean PointSmooth;
115    GLboolean PolygonOffsetPoint;
116    GLboolean PolygonOffsetLine;
117    GLboolean PolygonOffsetFill;
118    GLboolean PolygonSmooth;
119    GLboolean PolygonStipple;
120    GLboolean RescaleNormals;
121    GLboolean Scissor;
122    GLboolean Stencil;
123    GLboolean StencilTwoSide;          /* GL_EXT_stencil_two_side */
124    GLboolean MultisampleEnabled;      /* GL_ARB_multisample */
125    GLboolean SampleAlphaToCoverage;   /* GL_ARB_multisample */
126    GLboolean SampleAlphaToOne;        /* GL_ARB_multisample */
127    GLboolean SampleCoverage;          /* GL_ARB_multisample */
128    GLboolean SampleCoverageInvert;    /* GL_ARB_multisample */
129    GLboolean RasterPositionUnclipped; /* GL_IBM_rasterpos_clip */
130
131    GLbitfield Texture[MAX_TEXTURE_UNITS];
132    GLbitfield TexGen[MAX_TEXTURE_UNITS];
133
134    /* SGI_texture_color_table */
135    GLboolean TextureColorTable[MAX_TEXTURE_UNITS];
136
137    /* GL_ARB_vertex_program / GL_NV_vertex_program */
138    GLboolean VertexProgram;
139    GLboolean VertexProgramPointSize;
140    GLboolean VertexProgramTwoSide;
141
142    /* GL_ARB_point_sprite / GL_NV_point_sprite */
143    GLboolean PointSprite;
144    GLboolean FragmentShaderATI;
145 };
146
147
148 /**
149  * Node for the attribute stack.
150  */
151 struct gl_attrib_node
152 {
153    GLbitfield kind;
154    void *data;
155    struct gl_attrib_node *next;
156 };
157
158
159
160 /**
161  * Special struct for saving/restoring texture state (GL_TEXTURE_BIT)
162  */
163 struct texture_state
164 {
165    struct gl_texture_attrib Texture;  /**< The usual context state */
166
167    /** to save per texture object state (wrap modes, filters, etc): */
168    struct gl_texture_object SavedObj[MAX_TEXTURE_UNITS][NUM_TEXTURE_TARGETS];
169
170    /**
171     * To save references to texture objects (so they don't get accidentally
172     * deleted while saved in the attribute stack).
173     */
174    struct gl_texture_object *SavedTexRef[MAX_TEXTURE_UNITS][NUM_TEXTURE_TARGETS];
175 };
176
177
178 #if FEATURE_attrib_stack
179
180
181 /**
182  * Allocate new attribute node of given type/kind.  Attach payload data.
183  * Insert it into the linked list named by 'head'.
184  */
185 static void
186 save_attrib_data(struct gl_attrib_node **head,
187                  GLbitfield kind, void *payload)
188 {
189    struct gl_attrib_node *n = MALLOC_STRUCT(gl_attrib_node);
190    if (n) {
191       n->kind = kind;
192       n->data = payload;
193       /* insert at head */
194       n->next = *head;
195       *head = n;
196    }
197    else {
198       /* out of memory! */
199    }
200 }
201
202
203 void GLAPIENTRY
204 _mesa_PushAttrib(GLbitfield mask)
205 {
206    struct gl_attrib_node *head;
207
208    GET_CURRENT_CONTEXT(ctx);
209    ASSERT_OUTSIDE_BEGIN_END(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       for (i = 0; i < COLORTABLE_MAX; i++) {
267          attr->ColorTable[i] = ctx->Pixel.ColorTableEnabled[i];
268       }
269       attr->Convolution1D = ctx->Pixel.Convolution1DEnabled;
270       attr->Convolution2D = ctx->Pixel.Convolution2DEnabled;
271       attr->Separable2D = ctx->Pixel.Separable2DEnabled;
272       attr->CullFace = ctx->Polygon.CullFlag;
273       attr->DepthClamp = ctx->Transform.DepthClamp;
274       attr->DepthTest = ctx->Depth.Test;
275       attr->Dither = ctx->Color.DitherFlag;
276       attr->Fog = ctx->Fog.Enabled;
277       for (i = 0; i < ctx->Const.MaxLights; i++) {
278          attr->Light[i] = ctx->Light.Light[i].Enabled;
279       }
280       attr->Lighting = ctx->Light.Enabled;
281       attr->LineSmooth = ctx->Line.SmoothFlag;
282       attr->LineStipple = ctx->Line.StippleFlag;
283       attr->Histogram = ctx->Pixel.HistogramEnabled;
284       attr->MinMax = ctx->Pixel.MinMaxEnabled;
285       attr->IndexLogicOp = ctx->Color.IndexLogicOpEnabled;
286       attr->ColorLogicOp = ctx->Color.ColorLogicOpEnabled;
287       attr->Map1Color4 = ctx->Eval.Map1Color4;
288       attr->Map1Index = ctx->Eval.Map1Index;
289       attr->Map1Normal = ctx->Eval.Map1Normal;
290       attr->Map1TextureCoord1 = ctx->Eval.Map1TextureCoord1;
291       attr->Map1TextureCoord2 = ctx->Eval.Map1TextureCoord2;
292       attr->Map1TextureCoord3 = ctx->Eval.Map1TextureCoord3;
293       attr->Map1TextureCoord4 = ctx->Eval.Map1TextureCoord4;
294       attr->Map1Vertex3 = ctx->Eval.Map1Vertex3;
295       attr->Map1Vertex4 = ctx->Eval.Map1Vertex4;
296       MEMCPY(attr->Map1Attrib, ctx->Eval.Map1Attrib, sizeof(ctx->Eval.Map1Attrib));
297       attr->Map2Color4 = ctx->Eval.Map2Color4;
298       attr->Map2Index = ctx->Eval.Map2Index;
299       attr->Map2Normal = ctx->Eval.Map2Normal;
300       attr->Map2TextureCoord1 = ctx->Eval.Map2TextureCoord1;
301       attr->Map2TextureCoord2 = ctx->Eval.Map2TextureCoord2;
302       attr->Map2TextureCoord3 = ctx->Eval.Map2TextureCoord3;
303       attr->Map2TextureCoord4 = ctx->Eval.Map2TextureCoord4;
304       attr->Map2Vertex3 = ctx->Eval.Map2Vertex3;
305       attr->Map2Vertex4 = ctx->Eval.Map2Vertex4;
306       MEMCPY(attr->Map2Attrib, ctx->Eval.Map2Attrib, sizeof(ctx->Eval.Map2Attrib));
307       attr->Normalize = ctx->Transform.Normalize;
308       attr->RasterPositionUnclipped = ctx->Transform.RasterPositionUnclipped;
309       attr->PointSmooth = ctx->Point.SmoothFlag;
310       attr->PointSprite = ctx->Point.PointSprite;
311       attr->PolygonOffsetPoint = ctx->Polygon.OffsetPoint;
312       attr->PolygonOffsetLine = ctx->Polygon.OffsetLine;
313       attr->PolygonOffsetFill = ctx->Polygon.OffsetFill;
314       attr->PolygonSmooth = ctx->Polygon.SmoothFlag;
315       attr->PolygonStipple = ctx->Polygon.StippleFlag;
316       attr->RescaleNormals = ctx->Transform.RescaleNormals;
317       attr->Scissor = ctx->Scissor.Enabled;
318       attr->Stencil = ctx->Stencil.Enabled;
319       attr->StencilTwoSide = ctx->Stencil.TestTwoSide;
320       attr->MultisampleEnabled = ctx->Multisample.Enabled;
321       attr->SampleAlphaToCoverage = ctx->Multisample.SampleAlphaToCoverage;
322       attr->SampleAlphaToOne = ctx->Multisample.SampleAlphaToOne;
323       attr->SampleCoverage = ctx->Multisample.SampleCoverage;
324       attr->SampleCoverageInvert = ctx->Multisample.SampleCoverageInvert;
325       for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
326          attr->Texture[i] = ctx->Texture.Unit[i].Enabled;
327          attr->TexGen[i] = ctx->Texture.Unit[i].TexGenEnabled;
328          attr->TextureColorTable[i] = ctx->Texture.Unit[i].ColorTableEnabled;
329       }
330       /* GL_NV_vertex_program */
331       attr->VertexProgram = ctx->VertexProgram.Enabled;
332       attr->VertexProgramPointSize = ctx->VertexProgram.PointSizeEnabled;
333       attr->VertexProgramTwoSide = ctx->VertexProgram.TwoSideEnabled;
334       save_attrib_data(&head, GL_ENABLE_BIT, attr);
335    }
336
337    if (mask & GL_EVAL_BIT) {
338       struct gl_eval_attrib *attr;
339       attr = MALLOC_STRUCT( gl_eval_attrib );
340       MEMCPY( attr, &ctx->Eval, sizeof(struct gl_eval_attrib) );
341       save_attrib_data(&head, GL_EVAL_BIT, attr);
342    }
343
344    if (mask & GL_FOG_BIT) {
345       struct gl_fog_attrib *attr;
346       attr = MALLOC_STRUCT( gl_fog_attrib );
347       MEMCPY( attr, &ctx->Fog, sizeof(struct gl_fog_attrib) );
348       save_attrib_data(&head, GL_FOG_BIT, attr);
349    }
350
351    if (mask & GL_HINT_BIT) {
352       struct gl_hint_attrib *attr;
353       attr = MALLOC_STRUCT( gl_hint_attrib );
354       MEMCPY( attr, &ctx->Hint, sizeof(struct gl_hint_attrib) );
355       save_attrib_data(&head, GL_HINT_BIT, attr);
356    }
357
358    if (mask & GL_LIGHTING_BIT) {
359       struct gl_light_attrib *attr;
360       FLUSH_CURRENT(ctx, 0);    /* flush material changes */
361       attr = MALLOC_STRUCT( gl_light_attrib );
362       MEMCPY( attr, &ctx->Light, sizeof(struct gl_light_attrib) );
363       save_attrib_data(&head, GL_LIGHTING_BIT, attr);
364    }
365
366    if (mask & GL_LINE_BIT) {
367       struct gl_line_attrib *attr;
368       attr = MALLOC_STRUCT( gl_line_attrib );
369       MEMCPY( attr, &ctx->Line, sizeof(struct gl_line_attrib) );
370       save_attrib_data(&head, GL_LINE_BIT, attr);
371    }
372
373    if (mask & GL_LIST_BIT) {
374       struct gl_list_attrib *attr;
375       attr = MALLOC_STRUCT( gl_list_attrib );
376       MEMCPY( attr, &ctx->List, sizeof(struct gl_list_attrib) );
377       save_attrib_data(&head, GL_LIST_BIT, attr);
378    }
379
380    if (mask & GL_PIXEL_MODE_BIT) {
381       struct gl_pixel_attrib *attr;
382       attr = MALLOC_STRUCT( gl_pixel_attrib );
383       MEMCPY( attr, &ctx->Pixel, sizeof(struct gl_pixel_attrib) );
384       /* push the Read FBO's ReadBuffer state, not ctx->Pixel.ReadBuffer */
385       attr->ReadBuffer = ctx->ReadBuffer->ColorReadBuffer;
386       save_attrib_data(&head, GL_PIXEL_MODE_BIT, attr);
387    }
388
389    if (mask & GL_POINT_BIT) {
390       struct gl_point_attrib *attr;
391       attr = MALLOC_STRUCT( gl_point_attrib );
392       MEMCPY( attr, &ctx->Point, sizeof(struct gl_point_attrib) );
393       save_attrib_data(&head, GL_POINT_BIT, attr);
394    }
395
396    if (mask & GL_POLYGON_BIT) {
397       struct gl_polygon_attrib *attr;
398       attr = MALLOC_STRUCT( gl_polygon_attrib );
399       MEMCPY( attr, &ctx->Polygon, sizeof(struct gl_polygon_attrib) );
400       save_attrib_data(&head, GL_POLYGON_BIT, attr);
401    }
402
403    if (mask & GL_POLYGON_STIPPLE_BIT) {
404       GLuint *stipple;
405       stipple = (GLuint *) MALLOC( 32*sizeof(GLuint) );
406       MEMCPY( stipple, ctx->PolygonStipple, 32*sizeof(GLuint) );
407       save_attrib_data(&head, GL_POLYGON_STIPPLE_BIT, stipple);
408    }
409
410    if (mask & GL_SCISSOR_BIT) {
411       struct gl_scissor_attrib *attr;
412       attr = MALLOC_STRUCT( gl_scissor_attrib );
413       MEMCPY( attr, &ctx->Scissor, sizeof(struct gl_scissor_attrib) );
414       save_attrib_data(&head, GL_SCISSOR_BIT, attr);
415    }
416
417    if (mask & GL_STENCIL_BUFFER_BIT) {
418       struct gl_stencil_attrib *attr;
419       attr = MALLOC_STRUCT( gl_stencil_attrib );
420       MEMCPY( attr, &ctx->Stencil, sizeof(struct gl_stencil_attrib) );
421       save_attrib_data(&head, GL_STENCIL_BUFFER_BIT, attr);
422    }
423
424    if (mask & GL_TEXTURE_BIT) {
425       struct texture_state *texstate = CALLOC_STRUCT(texture_state);
426       GLuint u, tex;
427
428       if (!texstate) {
429          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib(GL_TEXTURE_BIT)");
430          goto end;
431       }
432
433       _mesa_lock_context_textures(ctx);
434
435       /* copy/save the bulk of texture state here */
436       _mesa_memcpy(&texstate->Texture, &ctx->Texture, sizeof(ctx->Texture));
437
438       /* Save references to the currently bound texture objects so they don't
439        * accidentally get deleted while referenced in the attribute stack.
440        */
441       for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
442          for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
443             _mesa_reference_texobj(&texstate->SavedTexRef[u][tex],
444                                    ctx->Texture.Unit[u].CurrentTex[tex]);
445          }
446       }
447
448       /* copy state/contents of the currently bound texture objects */
449       for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
450          for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
451             _mesa_copy_texture_object(&texstate->SavedObj[u][tex],
452                                       ctx->Texture.Unit[u].CurrentTex[tex]);
453          }
454       }
455
456       _mesa_unlock_context_textures(ctx);
457
458       save_attrib_data(&head, GL_TEXTURE_BIT, texstate);
459    }
460
461    if (mask & GL_TRANSFORM_BIT) {
462       struct gl_transform_attrib *attr;
463       attr = MALLOC_STRUCT( gl_transform_attrib );
464       MEMCPY( attr, &ctx->Transform, sizeof(struct gl_transform_attrib) );
465       save_attrib_data(&head, GL_TRANSFORM_BIT, attr);
466    }
467
468    if (mask & GL_VIEWPORT_BIT) {
469       struct gl_viewport_attrib *attr;
470       attr = MALLOC_STRUCT( gl_viewport_attrib );
471       MEMCPY( attr, &ctx->Viewport, sizeof(struct gl_viewport_attrib) );
472       save_attrib_data(&head, GL_VIEWPORT_BIT, attr);
473    }
474
475    /* GL_ARB_multisample */
476    if (mask & GL_MULTISAMPLE_BIT_ARB) {
477       struct gl_multisample_attrib *attr;
478       attr = MALLOC_STRUCT( gl_multisample_attrib );
479       MEMCPY( attr, &ctx->Multisample, sizeof(struct gl_multisample_attrib) );
480       save_attrib_data(&head, GL_MULTISAMPLE_BIT_ARB, attr);
481    }
482
483 end:
484    ctx->AttribStack[ctx->AttribStackDepth] = head;
485    ctx->AttribStackDepth++;
486 }
487
488
489
490 static void
491 pop_enable_group(GLcontext *ctx, const struct gl_enable_attrib *enable)
492 {
493    const GLuint curTexUnitSave = ctx->Texture.CurrentUnit;
494    GLuint i;
495
496 #define TEST_AND_UPDATE(VALUE, NEWVALUE, ENUM)          \
497         if ((VALUE) != (NEWVALUE)) {                    \
498            _mesa_set_enable( ctx, ENUM, (NEWVALUE) );   \
499         }
500
501    TEST_AND_UPDATE(ctx->Color.AlphaEnabled, enable->AlphaTest, GL_ALPHA_TEST);
502    TEST_AND_UPDATE(ctx->Color.BlendEnabled, enable->Blend, GL_BLEND);
503
504    for (i=0;i<MAX_CLIP_PLANES;i++) {
505       const GLuint mask = 1 << i;
506       if ((ctx->Transform.ClipPlanesEnabled & mask) != (enable->ClipPlanes & mask))
507           _mesa_set_enable(ctx, (GLenum) (GL_CLIP_PLANE0 + i),
508                            (GLboolean) ((enable->ClipPlanes & mask) ? GL_TRUE : GL_FALSE));
509    }
510
511    TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled, enable->ColorMaterial,
512                    GL_COLOR_MATERIAL);
513    TEST_AND_UPDATE(ctx->Pixel.ColorTableEnabled[COLORTABLE_PRECONVOLUTION],
514                    enable->ColorTable[COLORTABLE_PRECONVOLUTION],
515                    GL_COLOR_TABLE);
516    TEST_AND_UPDATE(ctx->Pixel.ColorTableEnabled[COLORTABLE_POSTCONVOLUTION],
517                    enable->ColorTable[COLORTABLE_POSTCONVOLUTION],
518                    GL_POST_CONVOLUTION_COLOR_TABLE);
519    TEST_AND_UPDATE(ctx->Pixel.ColorTableEnabled[COLORTABLE_POSTCOLORMATRIX],
520                    enable->ColorTable[COLORTABLE_POSTCOLORMATRIX],
521                    GL_POST_COLOR_MATRIX_COLOR_TABLE);
522    TEST_AND_UPDATE(ctx->Polygon.CullFlag, enable->CullFace, GL_CULL_FACE);
523    TEST_AND_UPDATE(ctx->Transform.DepthClamp, enable->DepthClamp,
524                    GL_DEPTH_CLAMP);
525    TEST_AND_UPDATE(ctx->Depth.Test, enable->DepthTest, GL_DEPTH_TEST);
526    TEST_AND_UPDATE(ctx->Color.DitherFlag, enable->Dither, GL_DITHER);
527    TEST_AND_UPDATE(ctx->Pixel.Convolution1DEnabled, enable->Convolution1D,
528                    GL_CONVOLUTION_1D);
529    TEST_AND_UPDATE(ctx->Pixel.Convolution2DEnabled, enable->Convolution2D,
530                    GL_CONVOLUTION_2D);
531    TEST_AND_UPDATE(ctx->Pixel.Separable2DEnabled, enable->Separable2D,
532                    GL_SEPARABLE_2D);
533    TEST_AND_UPDATE(ctx->Fog.Enabled, enable->Fog, GL_FOG);
534    TEST_AND_UPDATE(ctx->Light.Enabled, enable->Lighting, GL_LIGHTING);
535    TEST_AND_UPDATE(ctx->Line.SmoothFlag, enable->LineSmooth, GL_LINE_SMOOTH);
536    TEST_AND_UPDATE(ctx->Line.StippleFlag, enable->LineStipple,
537                    GL_LINE_STIPPLE);
538    TEST_AND_UPDATE(ctx->Color.IndexLogicOpEnabled, enable->IndexLogicOp,
539                    GL_INDEX_LOGIC_OP);
540    TEST_AND_UPDATE(ctx->Color.ColorLogicOpEnabled, enable->ColorLogicOp,
541                    GL_COLOR_LOGIC_OP);
542
543    TEST_AND_UPDATE(ctx->Eval.Map1Color4, enable->Map1Color4, GL_MAP1_COLOR_4);
544    TEST_AND_UPDATE(ctx->Eval.Map1Index, enable->Map1Index, GL_MAP1_INDEX);
545    TEST_AND_UPDATE(ctx->Eval.Map1Normal, enable->Map1Normal, GL_MAP1_NORMAL);
546    TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord1, enable->Map1TextureCoord1,
547                    GL_MAP1_TEXTURE_COORD_1);
548    TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord2, enable->Map1TextureCoord2,
549                    GL_MAP1_TEXTURE_COORD_2);
550    TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord3, enable->Map1TextureCoord3,
551                    GL_MAP1_TEXTURE_COORD_3);
552    TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord4, enable->Map1TextureCoord4,
553                    GL_MAP1_TEXTURE_COORD_4);
554    TEST_AND_UPDATE(ctx->Eval.Map1Vertex3, enable->Map1Vertex3,
555                    GL_MAP1_VERTEX_3);
556    TEST_AND_UPDATE(ctx->Eval.Map1Vertex4, enable->Map1Vertex4,
557                    GL_MAP1_VERTEX_4);
558    for (i = 0; i < 16; i++) {
559       TEST_AND_UPDATE(ctx->Eval.Map1Attrib[i], enable->Map1Attrib[i],
560                       GL_MAP1_VERTEX_ATTRIB0_4_NV + i);
561    }
562
563    TEST_AND_UPDATE(ctx->Eval.Map2Color4, enable->Map2Color4, GL_MAP2_COLOR_4);
564    TEST_AND_UPDATE(ctx->Eval.Map2Index, enable->Map2Index, GL_MAP2_INDEX);
565    TEST_AND_UPDATE(ctx->Eval.Map2Normal, enable->Map2Normal, GL_MAP2_NORMAL);
566    TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord1, enable->Map2TextureCoord1,
567                    GL_MAP2_TEXTURE_COORD_1);
568    TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord2, enable->Map2TextureCoord2,
569                    GL_MAP2_TEXTURE_COORD_2);
570    TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord3, enable->Map2TextureCoord3,
571                    GL_MAP2_TEXTURE_COORD_3);
572    TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord4, enable->Map2TextureCoord4,
573                    GL_MAP2_TEXTURE_COORD_4);
574    TEST_AND_UPDATE(ctx->Eval.Map2Vertex3, enable->Map2Vertex3,
575                    GL_MAP2_VERTEX_3);
576    TEST_AND_UPDATE(ctx->Eval.Map2Vertex4, enable->Map2Vertex4,
577                    GL_MAP2_VERTEX_4);
578    for (i = 0; i < 16; i++) {
579       TEST_AND_UPDATE(ctx->Eval.Map2Attrib[i], enable->Map2Attrib[i],
580                       GL_MAP2_VERTEX_ATTRIB0_4_NV + i);
581    }
582
583    TEST_AND_UPDATE(ctx->Eval.AutoNormal, enable->AutoNormal, GL_AUTO_NORMAL);
584    TEST_AND_UPDATE(ctx->Transform.Normalize, enable->Normalize, GL_NORMALIZE);
585    TEST_AND_UPDATE(ctx->Transform.RescaleNormals, enable->RescaleNormals,
586                    GL_RESCALE_NORMAL_EXT);
587    TEST_AND_UPDATE(ctx->Transform.RasterPositionUnclipped,
588                    enable->RasterPositionUnclipped,
589                    GL_RASTER_POSITION_UNCLIPPED_IBM);
590    TEST_AND_UPDATE(ctx->Point.SmoothFlag, enable->PointSmooth,
591                    GL_POINT_SMOOTH);
592    if (ctx->Extensions.NV_point_sprite || ctx->Extensions.ARB_point_sprite) {
593       TEST_AND_UPDATE(ctx->Point.PointSprite, enable->PointSprite,
594                       GL_POINT_SPRITE_NV);
595    }
596    TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, enable->PolygonOffsetPoint,
597                    GL_POLYGON_OFFSET_POINT);
598    TEST_AND_UPDATE(ctx->Polygon.OffsetLine, enable->PolygonOffsetLine,
599                    GL_POLYGON_OFFSET_LINE);
600    TEST_AND_UPDATE(ctx->Polygon.OffsetFill, enable->PolygonOffsetFill,
601                    GL_POLYGON_OFFSET_FILL);
602    TEST_AND_UPDATE(ctx->Polygon.SmoothFlag, enable->PolygonSmooth,
603                    GL_POLYGON_SMOOTH);
604    TEST_AND_UPDATE(ctx->Polygon.StippleFlag, enable->PolygonStipple,
605                    GL_POLYGON_STIPPLE);
606    TEST_AND_UPDATE(ctx->Scissor.Enabled, enable->Scissor, GL_SCISSOR_TEST);
607    TEST_AND_UPDATE(ctx->Stencil.Enabled, enable->Stencil, GL_STENCIL_TEST);
608    if (ctx->Extensions.EXT_stencil_two_side) {
609       TEST_AND_UPDATE(ctx->Stencil.TestTwoSide, enable->StencilTwoSide, GL_STENCIL_TEST_TWO_SIDE_EXT);
610    }
611    TEST_AND_UPDATE(ctx->Multisample.Enabled, enable->MultisampleEnabled,
612                    GL_MULTISAMPLE_ARB);
613    TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
614                    enable->SampleAlphaToCoverage,
615                    GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
616    TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
617                    enable->SampleAlphaToOne,
618                    GL_SAMPLE_ALPHA_TO_ONE_ARB);
619    TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
620                    enable->SampleCoverage,
621                    GL_SAMPLE_COVERAGE_ARB);
622    TEST_AND_UPDATE(ctx->Multisample.SampleCoverageInvert,
623                    enable->SampleCoverageInvert,
624                    GL_SAMPLE_COVERAGE_INVERT_ARB);
625    /* GL_ARB_vertex_program, GL_NV_vertex_program */
626    TEST_AND_UPDATE(ctx->VertexProgram.Enabled,
627                    enable->VertexProgram,
628                    GL_VERTEX_PROGRAM_ARB);
629    TEST_AND_UPDATE(ctx->VertexProgram.PointSizeEnabled,
630                    enable->VertexProgramPointSize,
631                    GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
632    TEST_AND_UPDATE(ctx->VertexProgram.TwoSideEnabled,
633                    enable->VertexProgramTwoSide,
634                    GL_VERTEX_PROGRAM_TWO_SIDE_ARB);
635
636 #undef TEST_AND_UPDATE
637
638    /* texture unit enables */
639    for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
640       const GLbitfield enabled = enable->Texture[i];
641       const GLbitfield genEnabled = enable->TexGen[i];
642
643       if (ctx->Texture.Unit[i].Enabled != enabled) {
644          _mesa_ActiveTextureARB(GL_TEXTURE0 + i);
645
646          _mesa_set_enable(ctx, GL_TEXTURE_1D,
647                           (enabled & TEXTURE_1D_BIT) ? GL_TRUE : GL_FALSE);
648          _mesa_set_enable(ctx, GL_TEXTURE_2D,
649                           (enabled & TEXTURE_2D_BIT) ? GL_TRUE : GL_FALSE);
650          _mesa_set_enable(ctx, GL_TEXTURE_3D,
651                           (enabled & TEXTURE_3D_BIT) ? GL_TRUE : GL_FALSE);
652          if (ctx->Extensions.NV_texture_rectangle) {
653             _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE_ARB,
654                              (enabled & TEXTURE_RECT_BIT) ? GL_TRUE : GL_FALSE);
655          }
656          if (ctx->Extensions.ARB_texture_cube_map) {
657             _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP,
658                              (enabled & TEXTURE_CUBE_BIT) ? GL_TRUE : GL_FALSE);
659          }
660          if (ctx->Extensions.MESA_texture_array) {
661             _mesa_set_enable(ctx, GL_TEXTURE_1D_ARRAY_EXT,
662                            (enabled & TEXTURE_1D_ARRAY_BIT) ? GL_TRUE : GL_FALSE);
663             _mesa_set_enable(ctx, GL_TEXTURE_2D_ARRAY_EXT,
664                            (enabled & TEXTURE_2D_ARRAY_BIT) ? GL_TRUE : GL_FALSE);
665          }
666       }
667
668       if (ctx->Texture.Unit[i].TexGenEnabled != genEnabled) {
669          _mesa_ActiveTextureARB(GL_TEXTURE0 + i);
670          _mesa_set_enable(ctx, GL_TEXTURE_GEN_S,
671                           (genEnabled & S_BIT) ? GL_TRUE : GL_FALSE);
672          _mesa_set_enable(ctx, GL_TEXTURE_GEN_T,
673                           (genEnabled & T_BIT) ? GL_TRUE : GL_FALSE);
674          _mesa_set_enable(ctx, GL_TEXTURE_GEN_R,
675                           (genEnabled & R_BIT) ? GL_TRUE : GL_FALSE);
676          _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q,
677                           (genEnabled & Q_BIT) ? GL_TRUE : GL_FALSE);
678       }
679
680       /* GL_SGI_texture_color_table */
681       ctx->Texture.Unit[i].ColorTableEnabled = enable->TextureColorTable[i];
682    }
683
684    _mesa_ActiveTextureARB(GL_TEXTURE0 + curTexUnitSave);
685 }
686
687
688 /**
689  * Pop/restore texture attribute/group state.
690  */
691 static void
692 pop_texture_group(GLcontext *ctx, struct texture_state *texstate)
693 {
694    GLuint u;
695
696    _mesa_lock_context_textures(ctx);
697
698    for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
699       const struct gl_texture_unit *unit = &texstate->Texture.Unit[u];
700       GLuint tgt;
701
702       _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + u);
703       _mesa_set_enable(ctx, GL_TEXTURE_1D,
704                        (unit->Enabled & TEXTURE_1D_BIT) ? GL_TRUE : GL_FALSE);
705       _mesa_set_enable(ctx, GL_TEXTURE_2D,
706                        (unit->Enabled & TEXTURE_2D_BIT) ? GL_TRUE : GL_FALSE);
707       _mesa_set_enable(ctx, GL_TEXTURE_3D,
708                        (unit->Enabled & TEXTURE_3D_BIT) ? GL_TRUE : GL_FALSE);
709       if (ctx->Extensions.ARB_texture_cube_map) {
710          _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP_ARB,
711                      (unit->Enabled & TEXTURE_CUBE_BIT) ? GL_TRUE : GL_FALSE);
712       }
713       if (ctx->Extensions.NV_texture_rectangle) {
714          _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE_NV,
715                      (unit->Enabled & TEXTURE_RECT_BIT) ? GL_TRUE : GL_FALSE);
716       }
717       if (ctx->Extensions.MESA_texture_array) {
718          _mesa_set_enable(ctx, GL_TEXTURE_1D_ARRAY_EXT,
719                  (unit->Enabled & TEXTURE_1D_ARRAY_BIT) ? GL_TRUE : GL_FALSE);
720          _mesa_set_enable(ctx, GL_TEXTURE_2D_ARRAY_EXT,
721                  (unit->Enabled & TEXTURE_2D_ARRAY_BIT) ? GL_TRUE : GL_FALSE);
722       }
723
724       if (ctx->Extensions.SGI_texture_color_table) {
725          _mesa_set_enable(ctx, GL_TEXTURE_COLOR_TABLE_SGI,
726                           unit->ColorTableEnabled);
727       }
728       _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->EnvMode);
729       _mesa_TexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, unit->EnvColor);
730       _mesa_TexGeni(GL_S, GL_TEXTURE_GEN_MODE, unit->GenS.Mode);
731       _mesa_TexGeni(GL_T, GL_TEXTURE_GEN_MODE, unit->GenT.Mode);
732       _mesa_TexGeni(GL_R, GL_TEXTURE_GEN_MODE, unit->GenR.Mode);
733       _mesa_TexGeni(GL_Q, GL_TEXTURE_GEN_MODE, unit->GenQ.Mode);
734       _mesa_TexGenfv(GL_S, GL_OBJECT_PLANE, unit->GenS.ObjectPlane);
735       _mesa_TexGenfv(GL_T, GL_OBJECT_PLANE, unit->GenT.ObjectPlane);
736       _mesa_TexGenfv(GL_R, GL_OBJECT_PLANE, unit->GenR.ObjectPlane);
737       _mesa_TexGenfv(GL_Q, GL_OBJECT_PLANE, unit->GenQ.ObjectPlane);
738       /* Eye plane done differently to avoid re-transformation */
739       {
740          struct gl_texture_unit *destUnit = &ctx->Texture.Unit[u];
741          COPY_4FV(destUnit->GenS.EyePlane, unit->GenS.EyePlane);
742          COPY_4FV(destUnit->GenT.EyePlane, unit->GenT.EyePlane);
743          COPY_4FV(destUnit->GenR.EyePlane, unit->GenR.EyePlane);
744          COPY_4FV(destUnit->GenQ.EyePlane, unit->GenQ.EyePlane);
745          if (ctx->Driver.TexGen) {
746             ctx->Driver.TexGen(ctx, GL_S, GL_EYE_PLANE, unit->GenS.EyePlane);
747             ctx->Driver.TexGen(ctx, GL_T, GL_EYE_PLANE, unit->GenT.EyePlane);
748             ctx->Driver.TexGen(ctx, GL_R, GL_EYE_PLANE, unit->GenR.EyePlane);
749             ctx->Driver.TexGen(ctx, GL_Q, GL_EYE_PLANE, unit->GenQ.EyePlane);
750          }
751       }
752       _mesa_set_enable(ctx, GL_TEXTURE_GEN_S,
753                        ((unit->TexGenEnabled & S_BIT) ? GL_TRUE : GL_FALSE));
754       _mesa_set_enable(ctx, GL_TEXTURE_GEN_T,
755                        ((unit->TexGenEnabled & T_BIT) ? GL_TRUE : GL_FALSE));
756       _mesa_set_enable(ctx, GL_TEXTURE_GEN_R,
757                        ((unit->TexGenEnabled & R_BIT) ? GL_TRUE : GL_FALSE));
758       _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q,
759                        ((unit->TexGenEnabled & Q_BIT) ? GL_TRUE : GL_FALSE));
760       if (ctx->Extensions.EXT_texture_lod_bias) {
761          _mesa_TexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
762                        GL_TEXTURE_LOD_BIAS_EXT, unit->LodBias);
763       }
764       if (ctx->Extensions.EXT_texture_env_combine ||
765           ctx->Extensions.ARB_texture_env_combine) {
766          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,
767                        unit->Combine.ModeRGB);
768          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,
769                        unit->Combine.ModeA);
770          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB,
771                        unit->Combine.SourceRGB[0]);
772          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB,
773                        unit->Combine.SourceRGB[1]);
774          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB,
775                        unit->Combine.SourceRGB[2]);
776          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA,
777                        unit->Combine.SourceA[0]);
778          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA,
779                        unit->Combine.SourceA[1]);
780          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA,
781                        unit->Combine.SourceA[2]);
782          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB,
783                        unit->Combine.OperandRGB[0]);
784          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB,
785                        unit->Combine.OperandRGB[1]);
786          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB,
787                        unit->Combine.OperandRGB[2]);
788          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA,
789                        unit->Combine.OperandA[0]);
790          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA,
791                        unit->Combine.OperandA[1]);
792          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA,
793                        unit->Combine.OperandA[2]);
794          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE,
795                        1 << unit->Combine.ScaleShiftRGB);
796          _mesa_TexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE,
797                        1 << unit->Combine.ScaleShiftA);
798       }
799
800       /* Restore texture object state for each target */
801       for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
802          const struct gl_texture_object *obj = NULL;
803          GLenum target;
804
805          obj = &texstate->SavedObj[u][tgt];
806
807          /* don't restore state for unsupported targets to prevent
808           * raising GL errors.
809           */
810          if (obj->Target == GL_TEXTURE_CUBE_MAP_ARB &&
811              !ctx->Extensions.ARB_texture_cube_map) {
812             continue;
813          }
814          else if (obj->Target == GL_TEXTURE_RECTANGLE_NV &&
815                   !ctx->Extensions.NV_texture_rectangle) {
816             continue;
817          }
818          else if ((obj->Target == GL_TEXTURE_1D_ARRAY_EXT ||
819                    obj->Target == GL_TEXTURE_2D_ARRAY_EXT) &&
820                   !ctx->Extensions.MESA_texture_array) {
821             continue;
822          }
823
824          target = obj->Target;
825
826          _mesa_BindTexture(target, obj->Name);
827
828          _mesa_TexParameterfv(target, GL_TEXTURE_BORDER_COLOR, obj->BorderColor);
829          _mesa_TexParameterf(target, GL_TEXTURE_PRIORITY, obj->Priority);
830          _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, obj->WrapS);
831          _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, obj->WrapT);
832          _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, obj->WrapR);
833          _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, obj->MinFilter);
834          _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, obj->MagFilter);
835          _mesa_TexParameterf(target, GL_TEXTURE_MIN_LOD, obj->MinLod);
836          _mesa_TexParameterf(target, GL_TEXTURE_MAX_LOD, obj->MaxLod);
837          _mesa_TexParameterf(target, GL_TEXTURE_LOD_BIAS, obj->LodBias);
838          _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, obj->BaseLevel);
839          if (target != GL_TEXTURE_RECTANGLE_ARB)
840             _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, obj->MaxLevel);
841          if (ctx->Extensions.EXT_texture_filter_anisotropic) {
842             _mesa_TexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
843                                 obj->MaxAnisotropy);
844          }
845          if (ctx->Extensions.ARB_shadow_ambient) {
846             _mesa_TexParameterf(target, GL_TEXTURE_COMPARE_FAIL_VALUE_ARB,
847                                 obj->CompareFailValue);
848          }
849       }
850
851       /* remove saved references to the texture objects */
852       for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
853          _mesa_reference_texobj(&texstate->SavedTexRef[u][tgt], NULL);
854       }
855    }
856
857    _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + texstate->Texture.CurrentUnit);
858
859    _mesa_unlock_context_textures(ctx);
860 }
861
862
863 /*
864  * This function is kind of long just because we have to call a lot
865  * of device driver functions to update device driver state.
866  *
867  * XXX As it is now, most of the pop-code calls immediate-mode Mesa functions
868  * in order to restore GL state.  This isn't terribly efficient but it
869  * ensures that dirty flags and any derived state gets updated correctly.
870  * We could at least check if the value to restore equals the current value
871  * and then skip the Mesa call.
872  */
873 void GLAPIENTRY
874 _mesa_PopAttrib(void)
875 {
876    struct gl_attrib_node *attr, *next;
877    GET_CURRENT_CONTEXT(ctx);
878    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
879
880    if (ctx->AttribStackDepth == 0) {
881       _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopAttrib" );
882       return;
883    }
884
885    ctx->AttribStackDepth--;
886    attr = ctx->AttribStack[ctx->AttribStackDepth];
887
888    while (attr) {
889
890       if (MESA_VERBOSE & VERBOSE_API) {
891          _mesa_debug(ctx, "glPopAttrib %s\n",
892                      _mesa_lookup_enum_by_nr(attr->kind));
893       }
894
895       switch (attr->kind) {
896          case GL_ACCUM_BUFFER_BIT:
897             {
898                const struct gl_accum_attrib *accum;
899                accum = (const struct gl_accum_attrib *) attr->data;
900                _mesa_ClearAccum(accum->ClearColor[0],
901                                 accum->ClearColor[1],
902                                 accum->ClearColor[2],
903                                 accum->ClearColor[3]);
904             }
905             break;
906          case GL_COLOR_BUFFER_BIT:
907             {
908                const struct gl_colorbuffer_attrib *color;
909                color = (const struct gl_colorbuffer_attrib *) attr->data;
910                _mesa_ClearIndex((GLfloat) color->ClearIndex);
911                _mesa_ClearColor(color->ClearColor[0],
912                                 color->ClearColor[1],
913                                 color->ClearColor[2],
914                                 color->ClearColor[3]);
915                _mesa_IndexMask(color->IndexMask);
916                _mesa_ColorMask((GLboolean) (color->ColorMask[0] != 0),
917                                (GLboolean) (color->ColorMask[1] != 0),
918                                (GLboolean) (color->ColorMask[2] != 0),
919                                (GLboolean) (color->ColorMask[3] != 0));
920                {
921                   /* Need to determine if more than one color output is
922                    * specified.  If so, call glDrawBuffersARB, else call
923                    * glDrawBuffer().  This is a subtle, but essential point
924                    * since GL_FRONT (for example) is illegal for the former
925                    * function, but legal for the later.
926                    */
927                   GLboolean multipleBuffers = GL_FALSE;
928                   GLuint i;
929
930                   for (i = 1; i < ctx->Const.MaxDrawBuffers; i++) {
931                      if (color->DrawBuffer[i] != GL_NONE) {
932                         multipleBuffers = GL_TRUE;
933                         break;
934                      }
935                   }
936                   /* Call the API_level functions, not _mesa_drawbuffers()
937                    * since we need to do error checking on the pop'd
938                    * GL_DRAW_BUFFER.
939                    * Ex: if GL_FRONT were pushed, but we're popping with a
940                    * user FBO bound, GL_FRONT will be illegal and we'll need
941                    * to record that error.  Per OpenGL ARB decision.
942                    */
943                   if (multipleBuffers)
944                      _mesa_DrawBuffersARB(ctx->Const.MaxDrawBuffers,
945                                           color->DrawBuffer);
946                   else
947                      _mesa_DrawBuffer(color->DrawBuffer[0]);
948                }
949                _mesa_set_enable(ctx, GL_ALPHA_TEST, color->AlphaEnabled);
950                _mesa_AlphaFunc(color->AlphaFunc, color->AlphaRef);
951                _mesa_set_enable(ctx, GL_BLEND, color->BlendEnabled);
952                _mesa_BlendFuncSeparateEXT(color->BlendSrcRGB,
953                                           color->BlendDstRGB,
954                                           color->BlendSrcA,
955                                           color->BlendDstA);
956                /* This special case is because glBlendEquationSeparateEXT
957                 * cannot take GL_LOGIC_OP as a parameter.
958                 */
959                if ( color->BlendEquationRGB == color->BlendEquationA ) {
960                   _mesa_BlendEquation(color->BlendEquationRGB);
961                }
962                else {
963                   _mesa_BlendEquationSeparateEXT(color->BlendEquationRGB,
964                                                  color->BlendEquationA);
965                }
966                _mesa_BlendColor(color->BlendColor[0],
967                                 color->BlendColor[1],
968                                 color->BlendColor[2],
969                                 color->BlendColor[3]);
970                _mesa_LogicOp(color->LogicOp);
971                _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP,
972                                 color->ColorLogicOpEnabled);
973                _mesa_set_enable(ctx, GL_INDEX_LOGIC_OP,
974                                 color->IndexLogicOpEnabled);
975                _mesa_set_enable(ctx, GL_DITHER, color->DitherFlag);
976             }
977             break;
978          case GL_CURRENT_BIT:
979             FLUSH_CURRENT( ctx, 0 );
980             MEMCPY( &ctx->Current, attr->data,
981                     sizeof(struct gl_current_attrib) );
982             break;
983          case GL_DEPTH_BUFFER_BIT:
984             {
985                const struct gl_depthbuffer_attrib *depth;
986                depth = (const struct gl_depthbuffer_attrib *) attr->data;
987                _mesa_DepthFunc(depth->Func);
988                _mesa_ClearDepth(depth->Clear);
989                _mesa_set_enable(ctx, GL_DEPTH_TEST, depth->Test);
990                _mesa_DepthMask(depth->Mask);
991             }
992             break;
993          case GL_ENABLE_BIT:
994             {
995                const struct gl_enable_attrib *enable;
996                enable = (const struct gl_enable_attrib *) attr->data;
997                pop_enable_group(ctx, enable);
998                ctx->NewState |= _NEW_ALL;
999             }
1000             break;
1001          case GL_EVAL_BIT:
1002             MEMCPY( &ctx->Eval, attr->data, sizeof(struct gl_eval_attrib) );
1003             ctx->NewState |= _NEW_EVAL;
1004             break;
1005          case GL_FOG_BIT:
1006             {
1007                const struct gl_fog_attrib *fog;
1008                fog = (const struct gl_fog_attrib *) attr->data;
1009                _mesa_set_enable(ctx, GL_FOG, fog->Enabled);
1010                _mesa_Fogfv(GL_FOG_COLOR, fog->Color);
1011                _mesa_Fogf(GL_FOG_DENSITY, fog->Density);
1012                _mesa_Fogf(GL_FOG_START, fog->Start);
1013                _mesa_Fogf(GL_FOG_END, fog->End);
1014                _mesa_Fogf(GL_FOG_INDEX, fog->Index);
1015                _mesa_Fogi(GL_FOG_MODE, fog->Mode);
1016             }
1017             break;
1018          case GL_HINT_BIT:
1019             {
1020                const struct gl_hint_attrib *hint;
1021                hint = (const struct gl_hint_attrib *) attr->data;
1022                _mesa_Hint(GL_PERSPECTIVE_CORRECTION_HINT,
1023                           hint->PerspectiveCorrection );
1024                _mesa_Hint(GL_POINT_SMOOTH_HINT, hint->PointSmooth);
1025                _mesa_Hint(GL_LINE_SMOOTH_HINT, hint->LineSmooth);
1026                _mesa_Hint(GL_POLYGON_SMOOTH_HINT, hint->PolygonSmooth);
1027                _mesa_Hint(GL_FOG_HINT, hint->Fog);
1028                _mesa_Hint(GL_CLIP_VOLUME_CLIPPING_HINT_EXT,
1029                           hint->ClipVolumeClipping);
1030                _mesa_Hint(GL_TEXTURE_COMPRESSION_HINT_ARB,
1031                           hint->TextureCompression);
1032             }
1033             break;
1034          case GL_LIGHTING_BIT:
1035             {
1036                GLuint i;
1037                const struct gl_light_attrib *light;
1038                light = (const struct gl_light_attrib *) attr->data;
1039                /* lighting enable */
1040                _mesa_set_enable(ctx, GL_LIGHTING, light->Enabled);
1041                /* per-light state */
1042                if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
1043                   _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
1044                
1045                for (i = 0; i < ctx->Const.MaxLights; i++) {
1046                   const struct gl_light *l = &light->Light[i];
1047                   _mesa_set_enable(ctx, GL_LIGHT0 + i, l->Enabled);
1048                   _mesa_light(ctx, i, GL_AMBIENT, l->Ambient);
1049                   _mesa_light(ctx, i, GL_DIFFUSE, l->Diffuse);
1050                   _mesa_light(ctx, i, GL_SPECULAR, l->Specular );
1051                   _mesa_light(ctx, i, GL_POSITION, l->EyePosition);
1052                   _mesa_light(ctx, i, GL_SPOT_DIRECTION, l->SpotDirection);
1053                   _mesa_light(ctx, i, GL_SPOT_EXPONENT, &l->SpotExponent);
1054                   _mesa_light(ctx, i, GL_SPOT_CUTOFF, &l->SpotCutoff);
1055                   _mesa_light(ctx, i, GL_CONSTANT_ATTENUATION,
1056                               &l->ConstantAttenuation);
1057                   _mesa_light(ctx, i, GL_LINEAR_ATTENUATION,
1058                               &l->LinearAttenuation);
1059                   _mesa_light(ctx, i, GL_QUADRATIC_ATTENUATION,
1060                               &l->QuadraticAttenuation);
1061                }
1062                /* light model */
1063                _mesa_LightModelfv(GL_LIGHT_MODEL_AMBIENT,
1064                                   light->Model.Ambient);
1065                _mesa_LightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,
1066                                  (GLfloat) light->Model.LocalViewer);
1067                _mesa_LightModelf(GL_LIGHT_MODEL_TWO_SIDE,
1068                                  (GLfloat) light->Model.TwoSide);
1069                _mesa_LightModelf(GL_LIGHT_MODEL_COLOR_CONTROL,
1070                                  (GLfloat) light->Model.ColorControl);
1071                /* shade model */
1072                _mesa_ShadeModel(light->ShadeModel);
1073                /* color material */
1074                _mesa_ColorMaterial(light->ColorMaterialFace,
1075                                    light->ColorMaterialMode);
1076                _mesa_set_enable(ctx, GL_COLOR_MATERIAL,
1077                                 light->ColorMaterialEnabled);
1078                /* materials */
1079                MEMCPY(&ctx->Light.Material, &light->Material,
1080                       sizeof(struct gl_material));
1081             }
1082             break;
1083          case GL_LINE_BIT:
1084             {
1085                const struct gl_line_attrib *line;
1086                line = (const struct gl_line_attrib *) attr->data;
1087                _mesa_set_enable(ctx, GL_LINE_SMOOTH, line->SmoothFlag);
1088                _mesa_set_enable(ctx, GL_LINE_STIPPLE, line->StippleFlag);
1089                _mesa_LineStipple(line->StippleFactor, line->StipplePattern);
1090                _mesa_LineWidth(line->Width);
1091             }
1092             break;
1093          case GL_LIST_BIT:
1094             MEMCPY( &ctx->List, attr->data, sizeof(struct gl_list_attrib) );
1095             break;
1096          case GL_PIXEL_MODE_BIT:
1097             MEMCPY( &ctx->Pixel, attr->data, sizeof(struct gl_pixel_attrib) );
1098             /* XXX what other pixel state needs to be set by function calls? */
1099             _mesa_ReadBuffer(ctx->Pixel.ReadBuffer);
1100             ctx->NewState |= _NEW_PIXEL;
1101             break;
1102          case GL_POINT_BIT:
1103             {
1104                const struct gl_point_attrib *point;
1105                point = (const struct gl_point_attrib *) attr->data;
1106                _mesa_PointSize(point->Size);
1107                _mesa_set_enable(ctx, GL_POINT_SMOOTH, point->SmoothFlag);
1108                if (ctx->Extensions.EXT_point_parameters) {
1109                   _mesa_PointParameterfv(GL_DISTANCE_ATTENUATION_EXT,
1110                                          point->Params);
1111                   _mesa_PointParameterf(GL_POINT_SIZE_MIN_EXT,
1112                                         point->MinSize);
1113                   _mesa_PointParameterf(GL_POINT_SIZE_MAX_EXT,
1114                                         point->MaxSize);
1115                   _mesa_PointParameterf(GL_POINT_FADE_THRESHOLD_SIZE_EXT,
1116                                         point->Threshold);
1117                }
1118                if (ctx->Extensions.NV_point_sprite
1119                    || ctx->Extensions.ARB_point_sprite) {
1120                   GLuint u;
1121                   for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
1122                      _mesa_TexEnvi(GL_POINT_SPRITE_NV, GL_COORD_REPLACE_NV,
1123                                    (GLint) point->CoordReplace[u]);
1124                   }
1125                   _mesa_set_enable(ctx, GL_POINT_SPRITE_NV,point->PointSprite);
1126                   if (ctx->Extensions.NV_point_sprite)
1127                      _mesa_PointParameteri(GL_POINT_SPRITE_R_MODE_NV,
1128                                            ctx->Point.SpriteRMode);
1129                   _mesa_PointParameterf(GL_POINT_SPRITE_COORD_ORIGIN,
1130                                         (GLfloat)ctx->Point.SpriteOrigin);
1131                }
1132             }
1133             break;
1134          case GL_POLYGON_BIT:
1135             {
1136                const struct gl_polygon_attrib *polygon;
1137                polygon = (const struct gl_polygon_attrib *) attr->data;
1138                _mesa_CullFace(polygon->CullFaceMode);
1139                _mesa_FrontFace(polygon->FrontFace);
1140                _mesa_PolygonMode(GL_FRONT, polygon->FrontMode);
1141                _mesa_PolygonMode(GL_BACK, polygon->BackMode);
1142                _mesa_PolygonOffset(polygon->OffsetFactor,
1143                                    polygon->OffsetUnits);
1144                _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, polygon->SmoothFlag);
1145                _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, polygon->StippleFlag);
1146                _mesa_set_enable(ctx, GL_CULL_FACE, polygon->CullFlag);
1147                _mesa_set_enable(ctx, GL_POLYGON_OFFSET_POINT,
1148                                 polygon->OffsetPoint);
1149                _mesa_set_enable(ctx, GL_POLYGON_OFFSET_LINE,
1150                                 polygon->OffsetLine);
1151                _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL,
1152                                 polygon->OffsetFill);
1153             }
1154             break;
1155          case GL_POLYGON_STIPPLE_BIT:
1156             MEMCPY( ctx->PolygonStipple, attr->data, 32*sizeof(GLuint) );
1157             ctx->NewState |= _NEW_POLYGONSTIPPLE;
1158             if (ctx->Driver.PolygonStipple)
1159                ctx->Driver.PolygonStipple( ctx, (const GLubyte *) attr->data );
1160             break;
1161          case GL_SCISSOR_BIT:
1162             {
1163                const struct gl_scissor_attrib *scissor;
1164                scissor = (const struct gl_scissor_attrib *) attr->data;
1165                _mesa_Scissor(scissor->X, scissor->Y,
1166                              scissor->Width, scissor->Height);
1167                _mesa_set_enable(ctx, GL_SCISSOR_TEST, scissor->Enabled);
1168             }
1169             break;
1170          case GL_STENCIL_BUFFER_BIT:
1171             {
1172                const struct gl_stencil_attrib *stencil;
1173                stencil = (const struct gl_stencil_attrib *) attr->data;
1174                _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
1175                _mesa_ClearStencil(stencil->Clear);
1176                if (ctx->Extensions.EXT_stencil_two_side) {
1177                   _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
1178                                    stencil->TestTwoSide);
1179                   _mesa_ActiveStencilFaceEXT(stencil->ActiveFace
1180                                              ? GL_BACK : GL_FRONT);
1181                }
1182                /* front state */
1183                _mesa_StencilFuncSeparate(GL_FRONT,
1184                                          stencil->Function[0],
1185                                          stencil->Ref[0],
1186                                          stencil->ValueMask[0]);
1187                _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
1188                _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
1189                                        stencil->ZFailFunc[0],
1190                                        stencil->ZPassFunc[0]);
1191                /* back state */
1192                _mesa_StencilFuncSeparate(GL_BACK,
1193                                          stencil->Function[1],
1194                                          stencil->Ref[1],
1195                                          stencil->ValueMask[1]);
1196                _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
1197                _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
1198                                        stencil->ZFailFunc[1],
1199                                        stencil->ZPassFunc[1]);
1200             }
1201             break;
1202          case GL_TRANSFORM_BIT:
1203             {
1204                GLuint i;
1205                const struct gl_transform_attrib *xform;
1206                xform = (const struct gl_transform_attrib *) attr->data;
1207                _mesa_MatrixMode(xform->MatrixMode);
1208                if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
1209                   _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
1210
1211                /* restore clip planes */
1212                for (i = 0; i < MAX_CLIP_PLANES; i++) {
1213                   const GLuint mask = 1 << i;
1214                   const GLfloat *eyePlane = xform->EyeUserPlane[i];
1215                   COPY_4V(ctx->Transform.EyeUserPlane[i], eyePlane);
1216                   if (xform->ClipPlanesEnabled & mask) {
1217                      _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
1218                   }
1219                   else {
1220                      _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
1221                   }
1222                   if (ctx->Driver.ClipPlane)
1223                      ctx->Driver.ClipPlane( ctx, GL_CLIP_PLANE0 + i, eyePlane );
1224                }
1225
1226                /* normalize/rescale */
1227                if (xform->Normalize != ctx->Transform.Normalize)
1228                   _mesa_set_enable(ctx, GL_NORMALIZE,ctx->Transform.Normalize);
1229                if (xform->RescaleNormals != ctx->Transform.RescaleNormals)
1230                   _mesa_set_enable(ctx, GL_RESCALE_NORMAL_EXT,
1231                                    ctx->Transform.RescaleNormals);
1232                if (xform->DepthClamp != ctx->Transform.DepthClamp)
1233                   _mesa_set_enable(ctx, GL_DEPTH_CLAMP,
1234                                    ctx->Transform.DepthClamp);
1235             }
1236             break;
1237          case GL_TEXTURE_BIT:
1238             /* Take care of texture object reference counters */
1239             {
1240                struct texture_state *texstate
1241                   = (struct texture_state *) attr->data;
1242                pop_texture_group(ctx, texstate);
1243                ctx->NewState |= _NEW_TEXTURE;
1244             }
1245             break;
1246          case GL_VIEWPORT_BIT:
1247             {
1248                const struct gl_viewport_attrib *vp;
1249                vp = (const struct gl_viewport_attrib *) attr->data;
1250                _mesa_Viewport(vp->X, vp->Y, vp->Width, vp->Height);
1251                _mesa_DepthRange(vp->Near, vp->Far);
1252             }
1253             break;
1254          case GL_MULTISAMPLE_BIT_ARB:
1255             {
1256                const struct gl_multisample_attrib *ms;
1257                ms = (const struct gl_multisample_attrib *) attr->data;
1258                _mesa_SampleCoverageARB(ms->SampleCoverageValue,
1259                                        ms->SampleCoverageInvert);
1260             }
1261             break;
1262
1263          default:
1264             _mesa_problem( ctx, "Bad attrib flag in PopAttrib");
1265             break;
1266       }
1267
1268       next = attr->next;
1269       FREE( attr->data );
1270       FREE( attr );
1271       attr = next;
1272    }
1273 }
1274
1275
1276 /**
1277  * Helper for incrementing/decrementing vertex buffer object reference
1278  * counts when pushing/popping the GL_CLIENT_VERTEX_ARRAY_BIT attribute group.
1279  */
1280 static void
1281 adjust_buffer_object_ref_counts(struct gl_array_object *arrayObj, GLint step)
1282 {
1283    GLuint i;
1284
1285    arrayObj->Vertex.BufferObj->RefCount += step;
1286    arrayObj->Weight.BufferObj->RefCount += step;
1287    arrayObj->Normal.BufferObj->RefCount += step;
1288    arrayObj->Color.BufferObj->RefCount += step;
1289    arrayObj->SecondaryColor.BufferObj->RefCount += step;
1290    arrayObj->FogCoord.BufferObj->RefCount += step;
1291    arrayObj->Index.BufferObj->RefCount += step;
1292    arrayObj->EdgeFlag.BufferObj->RefCount += step;
1293    for (i = 0; i < Elements(arrayObj->TexCoord); i++)
1294       arrayObj->TexCoord[i].BufferObj->RefCount += step;
1295    for (i = 0; i < Elements(arrayObj->VertexAttrib); i++)
1296       arrayObj->VertexAttrib[i].BufferObj->RefCount += step;
1297 }
1298
1299
1300 /**
1301  * Copy gl_pixelstore_attrib from src to dst, updating buffer
1302  * object refcounts.
1303  */
1304 static void
1305 copy_pixelstore(GLcontext *ctx,
1306                 struct gl_pixelstore_attrib *dst,
1307                 const struct gl_pixelstore_attrib *src)
1308 {
1309    dst->Alignment = src->Alignment;
1310    dst->RowLength = src->RowLength;
1311    dst->SkipPixels = src->SkipPixels;
1312    dst->SkipRows = src->SkipRows;
1313    dst->ImageHeight = src->ImageHeight;
1314    dst->SkipImages = src->SkipImages;
1315    dst->SwapBytes = src->SwapBytes;
1316    dst->LsbFirst = src->LsbFirst;
1317    dst->ClientStorage = src->ClientStorage;
1318    dst->Invert = src->Invert;
1319    _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
1320 }
1321
1322
1323 #define GL_CLIENT_PACK_BIT (1<<20)
1324 #define GL_CLIENT_UNPACK_BIT (1<<21)
1325
1326
1327 void GLAPIENTRY
1328 _mesa_PushClientAttrib(GLbitfield mask)
1329 {
1330    struct gl_attrib_node *head;
1331
1332    GET_CURRENT_CONTEXT(ctx);
1333    ASSERT_OUTSIDE_BEGIN_END(ctx);
1334
1335    if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
1336       _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushClientAttrib" );
1337       return;
1338    }
1339
1340    /* Build linked list of attribute nodes which save all attribute
1341     * groups specified by the mask.
1342     */
1343    head = NULL;
1344
1345    if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
1346       struct gl_pixelstore_attrib *attr;
1347       /* packing attribs */
1348       attr = CALLOC_STRUCT( gl_pixelstore_attrib );
1349       copy_pixelstore(ctx, attr, &ctx->Pack);
1350       save_attrib_data(&head, GL_CLIENT_PACK_BIT, attr);
1351       /* unpacking attribs */
1352       attr = CALLOC_STRUCT( gl_pixelstore_attrib );
1353       copy_pixelstore(ctx, attr, &ctx->Unpack);
1354       save_attrib_data(&head, GL_CLIENT_UNPACK_BIT, attr);
1355    }
1356
1357    if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1358       struct gl_array_attrib *attr;
1359       struct gl_array_object *obj;
1360
1361       attr = MALLOC_STRUCT( gl_array_attrib );
1362       obj = MALLOC_STRUCT( gl_array_object );
1363
1364 #if FEATURE_ARB_vertex_buffer_object
1365       /* increment ref counts since we're copying pointers to these objects */
1366       ctx->Array.ArrayBufferObj->RefCount++;
1367       ctx->Array.ElementArrayBufferObj->RefCount++;
1368 #endif
1369
1370       MEMCPY( attr, &ctx->Array, sizeof(struct gl_array_attrib) );
1371       MEMCPY( obj, ctx->Array.ArrayObj, sizeof(struct gl_array_object) );
1372
1373       attr->ArrayObj = obj;
1374
1375       save_attrib_data(&head, GL_CLIENT_VERTEX_ARRAY_BIT, attr);
1376
1377       /* bump reference counts on buffer objects */
1378       adjust_buffer_object_ref_counts(ctx->Array.ArrayObj, 1);
1379    }
1380
1381    ctx->ClientAttribStack[ctx->ClientAttribStackDepth] = head;
1382    ctx->ClientAttribStackDepth++;
1383 }
1384
1385
1386
1387
1388 void GLAPIENTRY
1389 _mesa_PopClientAttrib(void)
1390 {
1391    struct gl_attrib_node *node, *next;
1392
1393    GET_CURRENT_CONTEXT(ctx);
1394    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1395
1396    if (ctx->ClientAttribStackDepth == 0) {
1397       _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib" );
1398       return;
1399    }
1400
1401    ctx->ClientAttribStackDepth--;
1402    node = ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
1403
1404    while (node) {
1405       switch (node->kind) {
1406          case GL_CLIENT_PACK_BIT:
1407             {
1408                struct gl_pixelstore_attrib *store =
1409                   (struct gl_pixelstore_attrib *) node->data;
1410                copy_pixelstore(ctx, &ctx->Pack, store);
1411                _mesa_reference_buffer_object(ctx, &store->BufferObj, NULL);
1412             }
1413             ctx->NewState |= _NEW_PACKUNPACK;
1414             break;
1415          case GL_CLIENT_UNPACK_BIT:
1416             {
1417                struct gl_pixelstore_attrib *store =
1418                   (struct gl_pixelstore_attrib *) node->data;
1419                copy_pixelstore(ctx, &ctx->Unpack, store);
1420                _mesa_reference_buffer_object(ctx, &store->BufferObj, NULL);
1421             }
1422             ctx->NewState |= _NEW_PACKUNPACK;
1423             break;
1424          case GL_CLIENT_VERTEX_ARRAY_BIT: {
1425             struct gl_array_attrib * data =
1426               (struct gl_array_attrib *) node->data;
1427
1428             adjust_buffer_object_ref_counts(ctx->Array.ArrayObj, -1);
1429          
1430             ctx->Array.ActiveTexture = data->ActiveTexture;
1431             if (data->LockCount != 0)
1432                _mesa_LockArraysEXT(data->LockFirst, data->LockCount);
1433             else if (ctx->Array.LockCount)
1434                _mesa_UnlockArraysEXT();
1435
1436             _mesa_BindVertexArrayAPPLE( data->ArrayObj->Name );
1437             
1438 #if FEATURE_ARB_vertex_buffer_object
1439             _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB,
1440                                 data->ArrayBufferObj->Name);
1441             _mesa_BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
1442                                 data->ElementArrayBufferObj->Name);
1443 #endif
1444
1445             MEMCPY( ctx->Array.ArrayObj, data->ArrayObj,
1446                     sizeof( struct gl_array_object ) );
1447
1448             FREE( data->ArrayObj );
1449             
1450             /* FIXME: Should some bits in ctx->Array->NewState also be set
1451              * FIXME: here?  It seems like it should be set to inclusive-or
1452              * FIXME: of the old ArrayObj->_Enabled and the new _Enabled.
1453              */
1454
1455             ctx->NewState |= _NEW_ARRAY;
1456             break;
1457          }
1458          default:
1459             _mesa_problem( ctx, "Bad attrib flag in PopClientAttrib");
1460             break;
1461       }
1462
1463       next = node->next;
1464       FREE( node->data );
1465       FREE( node );
1466       node = next;
1467    }
1468 }
1469
1470
1471 void
1472 _mesa_init_attrib_dispatch(struct _glapi_table *disp)
1473 {
1474    SET_PopAttrib(disp, _mesa_PopAttrib);
1475    SET_PushAttrib(disp, _mesa_PushAttrib);
1476    SET_PopClientAttrib(disp, _mesa_PopClientAttrib);
1477    SET_PushClientAttrib(disp, _mesa_PushClientAttrib);
1478 }
1479
1480
1481 #endif /* FEATURE_attrib_stack */
1482
1483
1484 /**
1485  * Free any attribute state data that might be attached to the context.
1486  */
1487 void
1488 _mesa_free_attrib_data(GLcontext *ctx)
1489 {
1490    while (ctx->AttribStackDepth > 0) {
1491       struct gl_attrib_node *attr, *next;
1492
1493       ctx->AttribStackDepth--;
1494       attr = ctx->AttribStack[ctx->AttribStackDepth];
1495
1496       while (attr) {
1497          if (attr->kind == GL_TEXTURE_BIT) {
1498             struct texture_state *texstate = (struct texture_state*)attr->data;
1499             GLuint u, tgt;
1500             /* clear references to the saved texture objects */
1501             for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
1502                for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
1503                   _mesa_reference_texobj(&texstate->SavedTexRef[u][tgt], NULL);
1504                }
1505             }
1506          }
1507          else {
1508             /* any other chunks of state that requires special handling? */
1509          }
1510
1511          next = attr->next;
1512          _mesa_free(attr->data);
1513          _mesa_free(attr);
1514          attr = next;
1515       }
1516    }
1517 }
1518
1519
1520 void _mesa_init_attrib( GLcontext *ctx )
1521 {
1522    /* Renderer and client attribute stacks */
1523    ctx->AttribStackDepth = 0;
1524    ctx->ClientAttribStackDepth = 0;
1525 }