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