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