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