• 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 "util/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 "context.h"
35 #include "depth.h"
36 #include "enable.h"
37 #include "enums.h"
38 #include "fog.h"
39 #include "hint.h"
40 #include "light.h"
41 #include "lines.h"
42 #include "macros.h"
43 #include "matrix.h"
44 #include "multisample.h"
45 #include "pixelstore.h"
46 #include "points.h"
47 #include "polygon.h"
48 #include "shared.h"
49 #include "scissor.h"
50 #include "stencil.h"
51 #include "texobj.h"
52 #include "texparam.h"
53 #include "texstate.h"
54 #include "varray.h"
55 #include "viewport.h"
56 #include "mtypes.h"
57 #include "state.h"
58 #include "hash.h"
59 #include <stdbool.h>
60 #include "util/u_memory.h"
61 #include "api_exec_decl.h"
62 
63 #include "state_tracker/st_cb_texture.h"
64 #include "state_tracker/st_manager.h"
65 #include "state_tracker/st_sampler_view.h"
66 
67 static inline bool
copy_texture_attribs(struct gl_texture_object * dst,const struct gl_texture_object * src,gl_texture_index tex)68 copy_texture_attribs(struct gl_texture_object *dst,
69                      const struct gl_texture_object *src,
70                      gl_texture_index tex)
71 {
72    /* All pushed fields have no effect on texture buffers. */
73    if (tex == TEXTURE_BUFFER_INDEX)
74       return false;
75 
76    /* Sampler fields have no effect on MSAA textures. */
77    if (tex != TEXTURE_2D_MULTISAMPLE_INDEX &&
78        tex != TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX) {
79       memcpy(&dst->Sampler.Attrib, &src->Sampler.Attrib,
80              sizeof(src->Sampler.Attrib));
81    }
82    memcpy(&dst->Attrib, &src->Attrib, sizeof(src->Attrib));
83    return true;
84 }
85 
86 
87 void GLAPIENTRY
_mesa_PushAttrib(GLbitfield mask)88 _mesa_PushAttrib(GLbitfield mask)
89 {
90    struct gl_attrib_node *head;
91 
92    GET_CURRENT_CONTEXT(ctx);
93 
94    if (MESA_VERBOSE & VERBOSE_API)
95       _mesa_debug(ctx, "glPushAttrib %x\n", (int) mask);
96 
97    if (ctx->AttribStackDepth >= MAX_ATTRIB_STACK_DEPTH) {
98       _mesa_error(ctx, GL_STACK_OVERFLOW, "glPushAttrib");
99       return;
100    }
101 
102    head = ctx->AttribStack[ctx->AttribStackDepth];
103    if (unlikely(!head)) {
104       head = CALLOC_STRUCT(gl_attrib_node);
105       if (unlikely(!head)) {
106          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
107          return;
108       }
109       ctx->AttribStack[ctx->AttribStackDepth] = head;
110    }
111 
112    head->Mask = mask;
113    head->OldPopAttribStateMask = ctx->PopAttribState;
114 
115    if (mask & GL_ACCUM_BUFFER_BIT)
116       memcpy(&head->Accum, &ctx->Accum, sizeof(head->Accum));
117 
118    if (mask & GL_COLOR_BUFFER_BIT) {
119       memcpy(&head->Color, &ctx->Color, sizeof(struct gl_colorbuffer_attrib));
120       /* push the Draw FBO's DrawBuffer[] state, not ctx->Color.DrawBuffer[] */
121       for (unsigned i = 0; i < ctx->Const.MaxDrawBuffers; i ++)
122          head->Color.DrawBuffer[i] = ctx->DrawBuffer->ColorDrawBuffer[i];
123    }
124 
125    if (mask & GL_CURRENT_BIT) {
126       FLUSH_CURRENT(ctx, 0);
127       memcpy(&head->Current, &ctx->Current, sizeof(head->Current));
128    }
129 
130    if (mask & GL_DEPTH_BUFFER_BIT)
131       memcpy(&head->Depth, &ctx->Depth, sizeof(head->Depth));
132 
133    if (mask & GL_ENABLE_BIT) {
134       struct gl_enable_attrib_node *attr = &head->Enable;
135       GLuint i;
136 
137       /* Copy enable flags from all other attributes into the enable struct. */
138       attr->AlphaTest = ctx->Color.AlphaEnabled;
139       attr->AutoNormal = ctx->Eval.AutoNormal;
140       attr->Blend = ctx->Color.BlendEnabled;
141       attr->ClipPlanes = ctx->Transform.ClipPlanesEnabled;
142       attr->ColorMaterial = ctx->Light.ColorMaterialEnabled;
143       attr->CullFace = ctx->Polygon.CullFlag;
144       attr->DepthClampNear = ctx->Transform.DepthClampNear;
145       attr->DepthClampFar = ctx->Transform.DepthClampFar;
146       attr->DepthTest = ctx->Depth.Test;
147       attr->Dither = ctx->Color.DitherFlag;
148       attr->Fog = ctx->Fog.Enabled;
149       for (i = 0; i < ctx->Const.MaxLights; i++) {
150          attr->Light[i] = ctx->Light.Light[i].Enabled;
151       }
152       attr->Lighting = ctx->Light.Enabled;
153       attr->LineSmooth = ctx->Line.SmoothFlag;
154       attr->LineStipple = ctx->Line.StippleFlag;
155       attr->IndexLogicOp = ctx->Color.IndexLogicOpEnabled;
156       attr->ColorLogicOp = ctx->Color.ColorLogicOpEnabled;
157       attr->Map1Color4 = ctx->Eval.Map1Color4;
158       attr->Map1Index = ctx->Eval.Map1Index;
159       attr->Map1Normal = ctx->Eval.Map1Normal;
160       attr->Map1TextureCoord1 = ctx->Eval.Map1TextureCoord1;
161       attr->Map1TextureCoord2 = ctx->Eval.Map1TextureCoord2;
162       attr->Map1TextureCoord3 = ctx->Eval.Map1TextureCoord3;
163       attr->Map1TextureCoord4 = ctx->Eval.Map1TextureCoord4;
164       attr->Map1Vertex3 = ctx->Eval.Map1Vertex3;
165       attr->Map1Vertex4 = ctx->Eval.Map1Vertex4;
166       attr->Map2Color4 = ctx->Eval.Map2Color4;
167       attr->Map2Index = ctx->Eval.Map2Index;
168       attr->Map2Normal = ctx->Eval.Map2Normal;
169       attr->Map2TextureCoord1 = ctx->Eval.Map2TextureCoord1;
170       attr->Map2TextureCoord2 = ctx->Eval.Map2TextureCoord2;
171       attr->Map2TextureCoord3 = ctx->Eval.Map2TextureCoord3;
172       attr->Map2TextureCoord4 = ctx->Eval.Map2TextureCoord4;
173       attr->Map2Vertex3 = ctx->Eval.Map2Vertex3;
174       attr->Map2Vertex4 = ctx->Eval.Map2Vertex4;
175       attr->Normalize = ctx->Transform.Normalize;
176       attr->RasterPositionUnclipped = ctx->Transform.RasterPositionUnclipped;
177       attr->PointSmooth = ctx->Point.SmoothFlag;
178       attr->PointSprite = ctx->Point.PointSprite;
179       attr->PolygonOffsetPoint = ctx->Polygon.OffsetPoint;
180       attr->PolygonOffsetLine = ctx->Polygon.OffsetLine;
181       attr->PolygonOffsetFill = ctx->Polygon.OffsetFill;
182       attr->PolygonSmooth = ctx->Polygon.SmoothFlag;
183       attr->PolygonStipple = ctx->Polygon.StippleFlag;
184       attr->RescaleNormals = ctx->Transform.RescaleNormals;
185       attr->Scissor = ctx->Scissor.EnableFlags;
186       attr->Stencil = ctx->Stencil.Enabled;
187       attr->StencilTwoSide = ctx->Stencil.TestTwoSide;
188       attr->MultisampleEnabled = ctx->Multisample.Enabled;
189       attr->SampleAlphaToCoverage = ctx->Multisample.SampleAlphaToCoverage;
190       attr->SampleAlphaToOne = ctx->Multisample.SampleAlphaToOne;
191       attr->SampleCoverage = ctx->Multisample.SampleCoverage;
192       for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
193          attr->Texture[i] = ctx->Texture.FixedFuncUnit[i].Enabled;
194          attr->TexGen[i] = ctx->Texture.FixedFuncUnit[i].TexGenEnabled;
195       }
196       /* GL_ARB_vertex_program */
197       attr->VertexProgram = ctx->VertexProgram.Enabled;
198       attr->VertexProgramPointSize = ctx->VertexProgram.PointSizeEnabled;
199       attr->VertexProgramTwoSide = ctx->VertexProgram.TwoSideEnabled;
200 
201       /* GL_ARB_fragment_program */
202       attr->FragmentProgram = ctx->FragmentProgram.Enabled;
203 
204       /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
205       attr->sRGBEnabled = ctx->Color.sRGBEnabled;
206 
207       /* GL_NV_conservative_raster */
208       attr->ConservativeRasterization = ctx->ConservativeRasterization;
209    }
210 
211    if (mask & GL_EVAL_BIT)
212       memcpy(&head->Eval, &ctx->Eval, sizeof(head->Eval));
213 
214    if (mask & GL_FOG_BIT)
215       memcpy(&head->Fog, &ctx->Fog, sizeof(head->Fog));
216 
217    if (mask & GL_HINT_BIT)
218       memcpy(&head->Hint, &ctx->Hint, sizeof(head->Hint));
219 
220    if (mask & GL_LIGHTING_BIT) {
221       FLUSH_CURRENT(ctx, 0);   /* flush material changes */
222       memcpy(&head->Light, &ctx->Light, sizeof(head->Light));
223    }
224 
225    if (mask & GL_LINE_BIT)
226       memcpy(&head->Line, &ctx->Line, sizeof(head->Line));
227 
228    if (mask & GL_LIST_BIT)
229       memcpy(&head->List, &ctx->List, sizeof(head->List));
230 
231    if (mask & GL_PIXEL_MODE_BIT) {
232       memcpy(&head->Pixel, &ctx->Pixel, sizeof(struct gl_pixel_attrib));
233       /* push the Read FBO's ReadBuffer state, not ctx->Pixel.ReadBuffer */
234       head->Pixel.ReadBuffer = ctx->ReadBuffer->ColorReadBuffer;
235    }
236 
237    if (mask & GL_POINT_BIT)
238       memcpy(&head->Point, &ctx->Point, sizeof(head->Point));
239 
240    if (mask & GL_POLYGON_BIT)
241       memcpy(&head->Polygon, &ctx->Polygon, sizeof(head->Polygon));
242 
243    if (mask & GL_POLYGON_STIPPLE_BIT) {
244       memcpy(&head->PolygonStipple, &ctx->PolygonStipple,
245              sizeof(head->PolygonStipple));
246    }
247 
248    if (mask & GL_SCISSOR_BIT)
249       memcpy(&head->Scissor, &ctx->Scissor, sizeof(head->Scissor));
250 
251    if (mask & GL_STENCIL_BUFFER_BIT)
252       memcpy(&head->Stencil, &ctx->Stencil, sizeof(head->Stencil));
253 
254    if (mask & GL_TEXTURE_BIT) {
255       GLuint u, tex;
256 
257       _mesa_lock_context_textures(ctx);
258 
259       /* copy/save the bulk of texture state here */
260       head->Texture.CurrentUnit = ctx->Texture.CurrentUnit;
261       memcpy(&head->Texture.FixedFuncUnit, &ctx->Texture.FixedFuncUnit,
262              sizeof(ctx->Texture.FixedFuncUnit));
263 
264       /* Copy/save contents of default texture objects. They are almost
265        * always bound, so this can be done unconditionally.
266        *
267        * We save them separately, so that we don't have to save them in every
268        * texture unit where they are bound. This decreases CPU overhead.
269        */
270       for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
271          struct gl_texture_object *dst = &head->Texture.SavedDefaultObj[tex];
272          struct gl_texture_object *src = ctx->Shared->DefaultTex[tex];
273 
274          copy_texture_attribs(dst, src, tex);
275       }
276 
277       /* copy state/contents of the currently bound texture objects */
278       unsigned num_tex_used = ctx->Texture.NumCurrentTexUsed;
279       for (u = 0; u < num_tex_used; u++) {
280          head->Texture.LodBias[u] = ctx->Texture.Unit[u].LodBias;
281          head->Texture.LodBiasQuantized[u] = ctx->Texture.Unit[u].LodBiasQuantized;
282 
283          for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
284             struct gl_texture_object *dst = &head->Texture.SavedObj[u][tex];
285             struct gl_texture_object *src = ctx->Texture.Unit[u].CurrentTex[tex];
286 
287             dst->Name = src->Name;
288 
289             /* Default texture targets are saved separately above. */
290             if (src->Name != 0)
291                copy_texture_attribs(dst, src, tex);
292          }
293       }
294       head->Texture.NumTexSaved = num_tex_used;
295       _mesa_unlock_context_textures(ctx);
296    }
297 
298    if (mask & GL_TRANSFORM_BIT)
299       memcpy(&head->Transform, &ctx->Transform, sizeof(head->Transform));
300 
301    if (mask & GL_VIEWPORT_BIT) {
302       memcpy(&head->Viewport.ViewportArray, &ctx->ViewportArray,
303              sizeof(struct gl_viewport_attrib)*ctx->Const.MaxViewports);
304 
305       head->Viewport.SubpixelPrecisionBias[0] = ctx->SubpixelPrecisionBias[0];
306       head->Viewport.SubpixelPrecisionBias[1] = ctx->SubpixelPrecisionBias[1];
307    }
308 
309    /* GL_ARB_multisample */
310    if (mask & GL_MULTISAMPLE_BIT_ARB)
311       memcpy(&head->Multisample, &ctx->Multisample, sizeof(head->Multisample));
312 
313    ctx->AttribStackDepth++;
314    ctx->PopAttribState = 0;
315 }
316 
317 
318 #define TEST_AND_UPDATE(VALUE, NEWVALUE, ENUM) do {  \
319       if ((VALUE) != (NEWVALUE))                     \
320          _mesa_set_enable(ctx, ENUM, (NEWVALUE));    \
321    } while (0)
322 
323 #define TEST_AND_UPDATE_BIT(VALUE, NEW_VALUE, BIT, ENUM) do {                 \
324       if (((VALUE) & BITFIELD_BIT(BIT)) != ((NEW_VALUE) & BITFIELD_BIT(BIT))) \
325          _mesa_set_enable(ctx, ENUM, ((NEW_VALUE) >> (BIT)) & 0x1);           \
326    } while (0)
327 
328 #define TEST_AND_UPDATE_INDEX(VALUE, NEW_VALUE, INDEX, ENUM) do {                 \
329       if (((VALUE) & BITFIELD_BIT(INDEX)) != ((NEW_VALUE) & BITFIELD_BIT(INDEX))) \
330          _mesa_set_enablei(ctx, ENUM, INDEX, ((NEW_VALUE) >> (INDEX)) & 0x1);     \
331    } while (0)
332 
333 
334 static void
pop_enable_group(struct gl_context * ctx,const struct gl_enable_attrib_node * enable)335 pop_enable_group(struct gl_context *ctx, const struct gl_enable_attrib_node *enable)
336 {
337    GLuint i;
338 
339    TEST_AND_UPDATE(ctx->Color.AlphaEnabled, enable->AlphaTest, GL_ALPHA_TEST);
340    if (ctx->Color.BlendEnabled != enable->Blend) {
341       if (ctx->Extensions.EXT_draw_buffers2) {
342          for (unsigned i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
343             TEST_AND_UPDATE_INDEX(ctx->Color.BlendEnabled, enable->Blend,
344                                   i, GL_BLEND);
345          }
346       } else {
347          _mesa_set_enable(ctx, GL_BLEND, (enable->Blend & 1));
348       }
349    }
350 
351    if (ctx->Transform.ClipPlanesEnabled != enable->ClipPlanes) {
352       for (unsigned i = 0; i < ctx->Const.MaxClipPlanes; i++) {
353          TEST_AND_UPDATE_BIT(ctx->Transform.ClipPlanesEnabled,
354                              enable->ClipPlanes, i, GL_CLIP_PLANE0 + i);
355       }
356    }
357 
358    TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled, enable->ColorMaterial,
359                    GL_COLOR_MATERIAL);
360    TEST_AND_UPDATE(ctx->Polygon.CullFlag, enable->CullFace, GL_CULL_FACE);
361 
362    if (!ctx->Extensions.AMD_depth_clamp_separate) {
363       TEST_AND_UPDATE(ctx->Transform.DepthClampNear && ctx->Transform.DepthClampFar,
364                       enable->DepthClampNear && enable->DepthClampFar,
365                       GL_DEPTH_CLAMP);
366    } else {
367       TEST_AND_UPDATE(ctx->Transform.DepthClampNear, enable->DepthClampNear,
368                       GL_DEPTH_CLAMP_NEAR_AMD);
369       TEST_AND_UPDATE(ctx->Transform.DepthClampFar, enable->DepthClampFar,
370                       GL_DEPTH_CLAMP_FAR_AMD);
371    }
372 
373    TEST_AND_UPDATE(ctx->Depth.Test, enable->DepthTest, GL_DEPTH_TEST);
374    TEST_AND_UPDATE(ctx->Color.DitherFlag, enable->Dither, GL_DITHER);
375    TEST_AND_UPDATE(ctx->Fog.Enabled, enable->Fog, GL_FOG);
376    TEST_AND_UPDATE(ctx->Light.Enabled, enable->Lighting, GL_LIGHTING);
377    TEST_AND_UPDATE(ctx->Line.SmoothFlag, enable->LineSmooth, GL_LINE_SMOOTH);
378    TEST_AND_UPDATE(ctx->Line.StippleFlag, enable->LineStipple,
379                    GL_LINE_STIPPLE);
380    TEST_AND_UPDATE(ctx->Color.IndexLogicOpEnabled, enable->IndexLogicOp,
381                    GL_INDEX_LOGIC_OP);
382    TEST_AND_UPDATE(ctx->Color.ColorLogicOpEnabled, enable->ColorLogicOp,
383                    GL_COLOR_LOGIC_OP);
384 
385    TEST_AND_UPDATE(ctx->Eval.Map1Color4, enable->Map1Color4, GL_MAP1_COLOR_4);
386    TEST_AND_UPDATE(ctx->Eval.Map1Index, enable->Map1Index, GL_MAP1_INDEX);
387    TEST_AND_UPDATE(ctx->Eval.Map1Normal, enable->Map1Normal, GL_MAP1_NORMAL);
388    TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord1, enable->Map1TextureCoord1,
389                    GL_MAP1_TEXTURE_COORD_1);
390    TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord2, enable->Map1TextureCoord2,
391                    GL_MAP1_TEXTURE_COORD_2);
392    TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord3, enable->Map1TextureCoord3,
393                    GL_MAP1_TEXTURE_COORD_3);
394    TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord4, enable->Map1TextureCoord4,
395                    GL_MAP1_TEXTURE_COORD_4);
396    TEST_AND_UPDATE(ctx->Eval.Map1Vertex3, enable->Map1Vertex3,
397                    GL_MAP1_VERTEX_3);
398    TEST_AND_UPDATE(ctx->Eval.Map1Vertex4, enable->Map1Vertex4,
399                    GL_MAP1_VERTEX_4);
400 
401    TEST_AND_UPDATE(ctx->Eval.Map2Color4, enable->Map2Color4, GL_MAP2_COLOR_4);
402    TEST_AND_UPDATE(ctx->Eval.Map2Index, enable->Map2Index, GL_MAP2_INDEX);
403    TEST_AND_UPDATE(ctx->Eval.Map2Normal, enable->Map2Normal, GL_MAP2_NORMAL);
404    TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord1, enable->Map2TextureCoord1,
405                    GL_MAP2_TEXTURE_COORD_1);
406    TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord2, enable->Map2TextureCoord2,
407                    GL_MAP2_TEXTURE_COORD_2);
408    TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord3, enable->Map2TextureCoord3,
409                    GL_MAP2_TEXTURE_COORD_3);
410    TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord4, enable->Map2TextureCoord4,
411                    GL_MAP2_TEXTURE_COORD_4);
412    TEST_AND_UPDATE(ctx->Eval.Map2Vertex3, enable->Map2Vertex3,
413                    GL_MAP2_VERTEX_3);
414    TEST_AND_UPDATE(ctx->Eval.Map2Vertex4, enable->Map2Vertex4,
415                    GL_MAP2_VERTEX_4);
416 
417    TEST_AND_UPDATE(ctx->Eval.AutoNormal, enable->AutoNormal, GL_AUTO_NORMAL);
418    TEST_AND_UPDATE(ctx->Transform.Normalize, enable->Normalize, GL_NORMALIZE);
419    TEST_AND_UPDATE(ctx->Transform.RescaleNormals, enable->RescaleNormals,
420                    GL_RESCALE_NORMAL_EXT);
421    TEST_AND_UPDATE(ctx->Transform.RasterPositionUnclipped,
422                    enable->RasterPositionUnclipped,
423                    GL_RASTER_POSITION_UNCLIPPED_IBM);
424    TEST_AND_UPDATE(ctx->Point.SmoothFlag, enable->PointSmooth,
425                    GL_POINT_SMOOTH);
426    TEST_AND_UPDATE(ctx->Point.PointSprite, enable->PointSprite,
427                    GL_POINT_SPRITE);
428    TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, enable->PolygonOffsetPoint,
429                    GL_POLYGON_OFFSET_POINT);
430    TEST_AND_UPDATE(ctx->Polygon.OffsetLine, enable->PolygonOffsetLine,
431                    GL_POLYGON_OFFSET_LINE);
432    TEST_AND_UPDATE(ctx->Polygon.OffsetFill, enable->PolygonOffsetFill,
433                    GL_POLYGON_OFFSET_FILL);
434    TEST_AND_UPDATE(ctx->Polygon.SmoothFlag, enable->PolygonSmooth,
435                    GL_POLYGON_SMOOTH);
436    TEST_AND_UPDATE(ctx->Polygon.StippleFlag, enable->PolygonStipple,
437                    GL_POLYGON_STIPPLE);
438    if (ctx->Scissor.EnableFlags != enable->Scissor) {
439       unsigned i;
440 
441       for (i = 0; i < ctx->Const.MaxViewports; i++) {
442          TEST_AND_UPDATE_INDEX(ctx->Scissor.EnableFlags, enable->Scissor,
443                                i, GL_SCISSOR_TEST);
444       }
445    }
446    TEST_AND_UPDATE(ctx->Stencil.Enabled, enable->Stencil, GL_STENCIL_TEST);
447    if (ctx->Extensions.EXT_stencil_two_side) {
448       TEST_AND_UPDATE(ctx->Stencil.TestTwoSide, enable->StencilTwoSide,
449                       GL_STENCIL_TEST_TWO_SIDE_EXT);
450    }
451    TEST_AND_UPDATE(ctx->Multisample.Enabled, enable->MultisampleEnabled,
452                    GL_MULTISAMPLE_ARB);
453    TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
454                    enable->SampleAlphaToCoverage,
455                    GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
456    TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
457                    enable->SampleAlphaToOne,
458                    GL_SAMPLE_ALPHA_TO_ONE_ARB);
459    TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
460                    enable->SampleCoverage,
461                    GL_SAMPLE_COVERAGE_ARB);
462    /* GL_ARB_vertex_program */
463    TEST_AND_UPDATE(ctx->VertexProgram.Enabled,
464                    enable->VertexProgram,
465                    GL_VERTEX_PROGRAM_ARB);
466    TEST_AND_UPDATE(ctx->VertexProgram.PointSizeEnabled,
467                    enable->VertexProgramPointSize,
468                    GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
469    TEST_AND_UPDATE(ctx->VertexProgram.TwoSideEnabled,
470                    enable->VertexProgramTwoSide,
471                    GL_VERTEX_PROGRAM_TWO_SIDE_ARB);
472 
473    /* GL_ARB_fragment_program */
474    TEST_AND_UPDATE(ctx->FragmentProgram.Enabled,
475                    enable->FragmentProgram,
476                    GL_FRAGMENT_PROGRAM_ARB);
477 
478    /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
479    TEST_AND_UPDATE(ctx->Color.sRGBEnabled, enable->sRGBEnabled,
480                    GL_FRAMEBUFFER_SRGB);
481 
482    /* GL_NV_conservative_raster */
483    if (ctx->Extensions.NV_conservative_raster) {
484       TEST_AND_UPDATE(ctx->ConservativeRasterization,
485                       enable->ConservativeRasterization,
486                       GL_CONSERVATIVE_RASTERIZATION_NV);
487    }
488 
489    const unsigned curTexUnitSave = ctx->Texture.CurrentUnit;
490 
491    /* texture unit enables */
492    for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
493       const GLbitfield enabled = enable->Texture[i];
494       const GLbitfield gen_enabled = enable->TexGen[i];
495       const struct gl_fixedfunc_texture_unit *unit = &ctx->Texture.FixedFuncUnit[i];
496       const GLbitfield old_enabled = unit->Enabled;
497       const GLbitfield old_gen_enabled = unit->TexGenEnabled;
498 
499       if (old_enabled == enabled && old_gen_enabled == gen_enabled)
500          continue;
501 
502       ctx->Texture.CurrentUnit = i;
503 
504       if (old_enabled != enabled) {
505          TEST_AND_UPDATE_BIT(old_enabled, enabled, TEXTURE_1D_INDEX, GL_TEXTURE_1D);
506          TEST_AND_UPDATE_BIT(old_enabled, enabled, TEXTURE_2D_INDEX, GL_TEXTURE_2D);
507          TEST_AND_UPDATE_BIT(old_enabled, enabled, TEXTURE_3D_INDEX, GL_TEXTURE_3D);
508          if (ctx->Extensions.NV_texture_rectangle) {
509             TEST_AND_UPDATE_BIT(old_enabled, enabled, TEXTURE_RECT_INDEX,
510                                 GL_TEXTURE_RECTANGLE);
511          }
512          TEST_AND_UPDATE_BIT(old_enabled, enabled, TEXTURE_CUBE_INDEX,
513                              GL_TEXTURE_CUBE_MAP);
514       }
515 
516       if (old_gen_enabled != gen_enabled) {
517          TEST_AND_UPDATE_BIT(old_gen_enabled, gen_enabled, 0, GL_TEXTURE_GEN_S);
518          TEST_AND_UPDATE_BIT(old_gen_enabled, gen_enabled, 1, GL_TEXTURE_GEN_T);
519          TEST_AND_UPDATE_BIT(old_gen_enabled, gen_enabled, 2, GL_TEXTURE_GEN_R);
520          TEST_AND_UPDATE_BIT(old_gen_enabled, gen_enabled, 3, GL_TEXTURE_GEN_Q);
521       }
522    }
523 
524    ctx->Texture.CurrentUnit = curTexUnitSave;
525 }
526 
527 
528 /**
529  * Pop/restore texture attribute/group state.
530  */
531 static void
pop_texture_group(struct gl_context * ctx,struct gl_texture_attrib_node * texstate)532 pop_texture_group(struct gl_context *ctx, struct gl_texture_attrib_node *texstate)
533 {
534    GLuint u;
535 
536    _mesa_lock_context_textures(ctx);
537 
538    /* Restore fixed-function texture unit states. */
539    for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
540       const struct gl_fixedfunc_texture_unit *unit =
541          &texstate->FixedFuncUnit[u];
542       struct gl_fixedfunc_texture_unit *destUnit =
543          &ctx->Texture.FixedFuncUnit[u];
544 
545       ctx->Texture.CurrentUnit = u;
546 
547       /* Fast path for other drivers. */
548       memcpy(destUnit, unit, sizeof(*unit));
549       destUnit->_CurrentCombine = NULL;
550       ctx->Texture.Unit[u].LodBias = texstate->LodBias[u];
551       ctx->Texture.Unit[u].LodBiasQuantized = texstate->LodBiasQuantized[u];
552    }
553 
554    /* Restore saved textures. */
555    unsigned num_tex_saved = texstate->NumTexSaved;
556    for (u = 0; u < num_tex_saved; u++) {
557       gl_texture_index tgt;
558 
559       ctx->Texture.CurrentUnit = u;
560 
561       /* Restore texture object state for each target */
562       for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
563          const struct gl_texture_object *savedObj = &texstate->SavedObj[u][tgt];
564          struct gl_texture_object *texObj =
565             _mesa_get_tex_unit(ctx, u)->CurrentTex[tgt];
566          bool is_msaa = tgt == TEXTURE_2D_MULTISAMPLE_INDEX ||
567                         tgt == TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX;
568 
569          /* According to the OpenGL 4.6 Compatibility Profile specification,
570           * table 23.17, GL_TEXTURE_BINDING_2D_MULTISAMPLE and
571           * GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY do not belong in the
572           * texture attrib group.
573           */
574          if (!is_msaa && texObj->Name != savedObj->Name) {
575             /* We don't need to check whether the texture target is supported,
576              * because we wouldn't get in this conditional block if it wasn't.
577              */
578             _mesa_BindTexture_no_error(texObj->Target, savedObj->Name);
579             texObj = _mesa_get_tex_unit(ctx, u)->CurrentTex[tgt];
580          }
581 
582          /* Default texture object states are restored separately below. */
583          if (texObj->Name == 0)
584             continue;
585 
586          /* But in the MSAA case, where the currently-bound object is not the
587           * default state, we should still restore the saved default object's
588           * data when that's what was saved initially.
589           */
590          if (savedObj->Name == 0)
591             savedObj = &texstate->SavedDefaultObj[tgt];
592 
593          if (!copy_texture_attribs(texObj, savedObj, tgt))
594             continue;
595 
596          st_texture_release_all_sampler_views(st_context(ctx), texObj);
597       }
598    }
599 
600    /* Restore textures in units that were not used before glPushAttrib (thus
601     * they were not saved) but were used after glPushAttrib. Revert
602     * the bindings to Name = 0.
603     */
604    unsigned num_tex_changed = ctx->Texture.NumCurrentTexUsed;
605    for (u = num_tex_saved; u < num_tex_changed; u++) {
606       ctx->Texture.CurrentUnit = u;
607 
608       for (gl_texture_index tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
609          struct gl_texture_object *texObj =
610             _mesa_get_tex_unit(ctx, u)->CurrentTex[tgt];
611          bool is_msaa = tgt == TEXTURE_2D_MULTISAMPLE_INDEX ||
612                         tgt == TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX;
613 
614          /* According to the OpenGL 4.6 Compatibility Profile specification,
615           * table 23.17, GL_TEXTURE_BINDING_2D_MULTISAMPLE and
616           * GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY do not belong in the
617           * texture attrib group.
618           */
619          if (!is_msaa && texObj->Name != 0) {
620             /* We don't need to check whether the texture target is supported,
621              * because we wouldn't get in this conditional block if it wasn't.
622              */
623             _mesa_BindTexture_no_error(texObj->Target, 0);
624          }
625       }
626    }
627 
628    /* Restore default texture object states. */
629    for (gl_texture_index tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
630       struct gl_texture_object *dst = ctx->Shared->DefaultTex[tex];
631       const struct gl_texture_object *src = &texstate->SavedDefaultObj[tex];
632 
633       copy_texture_attribs(dst, src, tex);
634    }
635 
636    _mesa_ActiveTexture(GL_TEXTURE0_ARB + texstate->CurrentUnit);
637    _mesa_unlock_context_textures(ctx);
638 }
639 
640 
641 #define TEST_AND_CALL1(FIELD, CALL) do { \
642       if (ctx->FIELD != attr->FIELD)     \
643          _mesa_##CALL(attr->FIELD);      \
644    } while (0)
645 
646 #define TEST_AND_CALL1_SEL(FIELD, CALL, SEL) do { \
647       if (ctx->FIELD != attr->FIELD)              \
648          _mesa_##CALL(SEL, attr->FIELD);          \
649    } while (0)
650 
651 #define TEST_AND_CALL2(FIELD1, FIELD2, CALL) do {                     \
652       if (ctx->FIELD1 != attr->FIELD1 || ctx->FIELD2 != attr->FIELD2) \
653          _mesa_##CALL(attr->FIELD1, attr->FIELD2);                    \
654    } while (0)
655 
656 
657 /*
658  * This function is kind of long just because we have to call a lot
659  * of device driver functions to update device driver state.
660  *
661  * XXX As it is now, most of the pop-code calls immediate-mode Mesa functions
662  * in order to restore GL state.  This isn't terribly efficient but it
663  * ensures that dirty flags and any derived state gets updated correctly.
664  * We could at least check if the value to restore equals the current value
665  * and then skip the Mesa call.
666  */
667 void GLAPIENTRY
_mesa_PopAttrib(void)668 _mesa_PopAttrib(void)
669 {
670    struct gl_attrib_node *attr;
671    GET_CURRENT_CONTEXT(ctx);
672    FLUSH_VERTICES(ctx, 0, 0);
673 
674    if (ctx->AttribStackDepth == 0) {
675       _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopAttrib");
676       return;
677    }
678 
679    ctx->AttribStackDepth--;
680    attr = ctx->AttribStack[ctx->AttribStackDepth];
681 
682    unsigned mask = attr->Mask;
683 
684    /* Flush current attribs. This must be done before PopAttribState is
685     * applied.
686     */
687    if (mask & GL_CURRENT_BIT)
688       FLUSH_CURRENT(ctx, 0);
689 
690    /* Only restore states that have been changed since glPushAttrib. */
691    mask &= ctx->PopAttribState;
692 
693    if (mask & GL_ACCUM_BUFFER_BIT) {
694       _mesa_ClearAccum(attr->Accum.ClearColor[0],
695                        attr->Accum.ClearColor[1],
696                        attr->Accum.ClearColor[2],
697                        attr->Accum.ClearColor[3]);
698    }
699 
700    if (mask & GL_COLOR_BUFFER_BIT) {
701       TEST_AND_CALL1(Color.ClearIndex, ClearIndex);
702       _mesa_ClearColor(attr->Color.ClearColor.f[0],
703                        attr->Color.ClearColor.f[1],
704                        attr->Color.ClearColor.f[2],
705                        attr->Color.ClearColor.f[3]);
706       TEST_AND_CALL1(Color.IndexMask, IndexMask);
707       if (ctx->Color.ColorMask != attr->Color.ColorMask) {
708          if (!ctx->Extensions.EXT_draw_buffers2) {
709             _mesa_ColorMask(GET_COLORMASK_BIT(attr->Color.ColorMask, 0, 0),
710                             GET_COLORMASK_BIT(attr->Color.ColorMask, 0, 1),
711                             GET_COLORMASK_BIT(attr->Color.ColorMask, 0, 2),
712                             GET_COLORMASK_BIT(attr->Color.ColorMask, 0, 3));
713          } else {
714             for (unsigned i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
715                _mesa_ColorMaski(i,
716                                 GET_COLORMASK_BIT(attr->Color.ColorMask, i, 0),
717                                 GET_COLORMASK_BIT(attr->Color.ColorMask, i, 1),
718                                 GET_COLORMASK_BIT(attr->Color.ColorMask, i, 2),
719                                 GET_COLORMASK_BIT(attr->Color.ColorMask, i, 3));
720             }
721          }
722       }
723       if (memcmp(ctx->Color.DrawBuffer, attr->Color.DrawBuffer,
724                  sizeof(attr->Color.DrawBuffer))) {
725          /* Need to determine if more than one color output is
726           * specified.  If so, call glDrawBuffersARB, else call
727           * glDrawBuffer().  This is a subtle, but essential point
728           * since GL_FRONT (for example) is illegal for the former
729           * function, but legal for the later.
730           */
731          GLboolean multipleBuffers = GL_FALSE;
732          GLuint i;
733 
734          for (i = 1; i < ctx->Const.MaxDrawBuffers; i++) {
735             if (attr->Color.DrawBuffer[i] != GL_NONE) {
736                multipleBuffers = GL_TRUE;
737                break;
738             }
739          }
740          /* Call the API_level functions, not _mesa_drawbuffers()
741           * since we need to do error checking on the pop'd
742           * GL_DRAW_BUFFER.
743           * Ex: if GL_FRONT were pushed, but we're popping with a
744           * user FBO bound, GL_FRONT will be illegal and we'll need
745           * to record that error.  Per OpenGL ARB decision.
746           */
747          if (multipleBuffers) {
748             GLenum buffers[MAX_DRAW_BUFFERS];
749 
750             for (unsigned i = 0; i < ctx->Const.MaxDrawBuffers; i++)
751                buffers[i] = attr->Color.DrawBuffer[i];
752 
753             _mesa_DrawBuffers(ctx->Const.MaxDrawBuffers, buffers);
754          } else {
755             _mesa_DrawBuffer(attr->Color.DrawBuffer[0]);
756          }
757       }
758       TEST_AND_UPDATE(ctx->Color.AlphaEnabled, attr->Color.AlphaEnabled,
759                       GL_ALPHA_TEST);
760       TEST_AND_CALL2(Color.AlphaFunc, Color.AlphaRefUnclamped, AlphaFunc);
761       if (ctx->Color.BlendEnabled != attr->Color.BlendEnabled) {
762          if (ctx->Extensions.EXT_draw_buffers2) {
763             for (unsigned i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
764                TEST_AND_UPDATE_INDEX(ctx->Color.BlendEnabled,
765                                      attr->Color.BlendEnabled, i, GL_BLEND);
766             }
767          }
768          else {
769             TEST_AND_UPDATE(ctx->Color.BlendEnabled & 0x1,
770                             attr->Color.BlendEnabled & 0x1, GL_BLEND);
771          }
772       }
773       if (ctx->Color._BlendFuncPerBuffer ||
774           ctx->Color._BlendEquationPerBuffer) {
775          /* set blend per buffer */
776          GLuint buf;
777          for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) {
778             _mesa_BlendFuncSeparateiARB(buf, attr->Color.Blend[buf].SrcRGB,
779                                      attr->Color.Blend[buf].DstRGB,
780                                      attr->Color.Blend[buf].SrcA,
781                                      attr->Color.Blend[buf].DstA);
782             _mesa_BlendEquationSeparateiARB(buf,
783                                          attr->Color.Blend[buf].EquationRGB,
784                                          attr->Color.Blend[buf].EquationA);
785          }
786       }
787       else {
788          /* set same blend modes for all buffers */
789          _mesa_BlendFuncSeparate(attr->Color.Blend[0].SrcRGB,
790                                     attr->Color.Blend[0].DstRGB,
791                                     attr->Color.Blend[0].SrcA,
792                                     attr->Color.Blend[0].DstA);
793          /* This special case is because glBlendEquationSeparateEXT
794           * cannot take GL_LOGIC_OP as a parameter.
795           */
796          if (attr->Color.Blend[0].EquationRGB ==
797              attr->Color.Blend[0].EquationA) {
798             TEST_AND_CALL1(Color.Blend[0].EquationRGB, BlendEquation);
799          }
800          else {
801             TEST_AND_CALL2(Color.Blend[0].EquationRGB,
802                            Color.Blend[0].EquationA, BlendEquationSeparate);
803          }
804       }
805       _mesa_BlendColor(attr->Color.BlendColorUnclamped[0],
806                        attr->Color.BlendColorUnclamped[1],
807                        attr->Color.BlendColorUnclamped[2],
808                        attr->Color.BlendColorUnclamped[3]);
809       TEST_AND_CALL1(Color.LogicOp, LogicOp);
810       TEST_AND_UPDATE(ctx->Color.ColorLogicOpEnabled,
811                       attr->Color.ColorLogicOpEnabled, GL_COLOR_LOGIC_OP);
812       TEST_AND_UPDATE(ctx->Color.IndexLogicOpEnabled,
813                       attr->Color.IndexLogicOpEnabled, GL_INDEX_LOGIC_OP);
814       TEST_AND_UPDATE(ctx->Color.DitherFlag, attr->Color.DitherFlag,
815                       GL_DITHER);
816       if (ctx->Extensions.ARB_color_buffer_float) {
817          TEST_AND_CALL1_SEL(Color.ClampFragmentColor, ClampColor,
818                             GL_CLAMP_FRAGMENT_COLOR);
819       }
820       if (ctx->Extensions.ARB_color_buffer_float || ctx->Version >= 30) {
821          TEST_AND_CALL1_SEL(Color.ClampReadColor, ClampColor,
822                             GL_CLAMP_READ_COLOR);
823       }
824       /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
825       if (ctx->Extensions.EXT_framebuffer_sRGB) {
826          TEST_AND_UPDATE(ctx->Color.sRGBEnabled, attr->Color.sRGBEnabled,
827                          GL_FRAMEBUFFER_SRGB);
828       }
829    }
830 
831    if (mask & GL_CURRENT_BIT) {
832       memcpy(&ctx->Current, &attr->Current,
833              sizeof(struct gl_current_attrib));
834       ctx->NewState |= _NEW_CURRENT_ATTRIB;
835    }
836 
837    if (mask & GL_DEPTH_BUFFER_BIT) {
838       TEST_AND_CALL1(Depth.Func, DepthFunc);
839       TEST_AND_CALL1(Depth.Clear, ClearDepth);
840       TEST_AND_UPDATE(ctx->Depth.Test, attr->Depth.Test, GL_DEPTH_TEST);
841       TEST_AND_CALL1(Depth.Mask, DepthMask);
842       if (ctx->Extensions.EXT_depth_bounds_test) {
843          TEST_AND_UPDATE(ctx->Depth.BoundsTest, attr->Depth.BoundsTest,
844                          GL_DEPTH_BOUNDS_TEST_EXT);
845          TEST_AND_CALL2(Depth.BoundsMin, Depth.BoundsMax, DepthBoundsEXT);
846       }
847    }
848 
849    if (mask & GL_ENABLE_BIT)
850       pop_enable_group(ctx, &attr->Enable);
851 
852    if (mask & GL_EVAL_BIT) {
853       memcpy(&ctx->Eval, &attr->Eval, sizeof(struct gl_eval_attrib));
854       vbo_exec_update_eval_maps(ctx);
855    }
856 
857    if (mask & GL_FOG_BIT) {
858       TEST_AND_UPDATE(ctx->Fog.Enabled, attr->Fog.Enabled, GL_FOG);
859       _mesa_Fogfv(GL_FOG_COLOR, attr->Fog.Color);
860       TEST_AND_CALL1_SEL(Fog.Density, Fogf, GL_FOG_DENSITY);
861       TEST_AND_CALL1_SEL(Fog.Start, Fogf, GL_FOG_START);
862       TEST_AND_CALL1_SEL(Fog.End, Fogf, GL_FOG_END);
863       TEST_AND_CALL1_SEL(Fog.Index, Fogf, GL_FOG_INDEX);
864       TEST_AND_CALL1_SEL(Fog.Mode, Fogi, GL_FOG_MODE);
865    }
866 
867    if (mask & GL_HINT_BIT) {
868       TEST_AND_CALL1_SEL(Hint.PerspectiveCorrection, Hint, GL_PERSPECTIVE_CORRECTION_HINT);
869       TEST_AND_CALL1_SEL(Hint.PointSmooth, Hint, GL_POINT_SMOOTH_HINT);
870       TEST_AND_CALL1_SEL(Hint.LineSmooth, Hint, GL_LINE_SMOOTH_HINT);
871       TEST_AND_CALL1_SEL(Hint.PolygonSmooth, Hint, GL_POLYGON_SMOOTH_HINT);
872       TEST_AND_CALL1_SEL(Hint.Fog, Hint, GL_FOG_HINT);
873       TEST_AND_CALL1_SEL(Hint.TextureCompression, Hint, GL_TEXTURE_COMPRESSION_HINT_ARB);
874    }
875 
876    if (mask & GL_LIGHTING_BIT) {
877       GLuint i;
878       /* lighting enable */
879       TEST_AND_UPDATE(ctx->Light.Enabled, attr->Light.Enabled, GL_LIGHTING);
880       /* per-light state */
881       if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
882          _math_matrix_analyse(ctx->ModelviewMatrixStack.Top);
883 
884       /* Fast path for other drivers. */
885       ctx->NewState |= _NEW_LIGHT_CONSTANTS | _NEW_FF_VERT_PROGRAM;
886 
887       memcpy(ctx->Light.LightSource, attr->Light.LightSource,
888              sizeof(attr->Light.LightSource));
889       memcpy(&ctx->Light.Model, &attr->Light.Model,
890              sizeof(attr->Light.Model));
891 
892       for (i = 0; i < ctx->Const.MaxLights; i++) {
893          TEST_AND_UPDATE(ctx->Light.Light[i].Enabled,
894                          attr->Light.Light[i].Enabled,
895                          GL_LIGHT0 + i);
896          memcpy(&ctx->Light.Light[i], &attr->Light.Light[i],
897                 sizeof(struct gl_light));
898       }
899       /* shade model */
900       TEST_AND_CALL1(Light.ShadeModel, ShadeModel);
901       /* color material */
902       TEST_AND_CALL2(Light.ColorMaterialFace, Light.ColorMaterialMode,
903                      ColorMaterial);
904       TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled,
905                       attr->Light.ColorMaterialEnabled, GL_COLOR_MATERIAL);
906       /* Shininess material is used by the fixed-func vertex program. */
907       ctx->NewState |= _NEW_MATERIAL | _NEW_FF_VERT_PROGRAM;
908       memcpy(&ctx->Light.Material, &attr->Light.Material,
909              sizeof(struct gl_material));
910       if (ctx->Extensions.ARB_color_buffer_float) {
911          TEST_AND_CALL1_SEL(Light.ClampVertexColor, ClampColor, GL_CLAMP_VERTEX_COLOR_ARB);
912       }
913    }
914 
915    if (mask & GL_LINE_BIT) {
916       TEST_AND_UPDATE(ctx->Line.SmoothFlag, attr->Line.SmoothFlag, GL_LINE_SMOOTH);
917       TEST_AND_UPDATE(ctx->Line.StippleFlag, attr->Line.StippleFlag, GL_LINE_STIPPLE);
918       TEST_AND_CALL2(Line.StippleFactor, Line.StipplePattern, LineStipple);
919       TEST_AND_CALL1(Line.Width, LineWidth);
920    }
921 
922    if (mask & GL_LIST_BIT)
923       memcpy(&ctx->List, &attr->List, sizeof(struct gl_list_attrib));
924 
925    if (mask & GL_PIXEL_MODE_BIT) {
926       memcpy(&ctx->Pixel, &attr->Pixel, sizeof(struct gl_pixel_attrib));
927       /* XXX what other pixel state needs to be set by function calls? */
928       _mesa_ReadBuffer(ctx->Pixel.ReadBuffer);
929       ctx->NewState |= _NEW_PIXEL;
930    }
931 
932    if (mask & GL_POINT_BIT) {
933       TEST_AND_CALL1(Point.Size, PointSize);
934       TEST_AND_UPDATE(ctx->Point.SmoothFlag, attr->Point.SmoothFlag, GL_POINT_SMOOTH);
935       _mesa_PointParameterfv(GL_DISTANCE_ATTENUATION_EXT, attr->Point.Params);
936       TEST_AND_CALL1_SEL(Point.MinSize, PointParameterf, GL_POINT_SIZE_MIN_EXT);
937       TEST_AND_CALL1_SEL(Point.MaxSize, PointParameterf, GL_POINT_SIZE_MAX_EXT);
938       TEST_AND_CALL1_SEL(Point.Threshold, PointParameterf, GL_POINT_FADE_THRESHOLD_SIZE_EXT);
939 
940       if (ctx->Point.CoordReplace != attr->Point.CoordReplace) {
941          ctx->NewState |= _NEW_POINT | _NEW_FF_VERT_PROGRAM;
942          ctx->Point.CoordReplace = attr->Point.CoordReplace;
943       }
944       TEST_AND_UPDATE(ctx->Point.PointSprite, attr->Point.PointSprite,
945                       GL_POINT_SPRITE);
946 
947       if ((_mesa_is_desktop_gl_compat(ctx) && ctx->Version >= 20)
948           || _mesa_is_desktop_gl_core(ctx))
949          TEST_AND_CALL1_SEL(Point.SpriteOrigin, PointParameterf, GL_POINT_SPRITE_COORD_ORIGIN);
950    }
951 
952    if (mask & GL_POLYGON_BIT) {
953       TEST_AND_CALL1(Polygon.CullFaceMode, CullFace);
954       TEST_AND_CALL1(Polygon.FrontFace, FrontFace);
955       TEST_AND_CALL1_SEL(Polygon.FrontMode, PolygonMode, GL_FRONT);
956       TEST_AND_CALL1_SEL(Polygon.BackMode, PolygonMode, GL_BACK);
957       _mesa_polygon_offset_clamp(ctx,
958                                  attr->Polygon.OffsetFactor,
959                                  attr->Polygon.OffsetUnits,
960                                  attr->Polygon.OffsetClamp);
961       TEST_AND_UPDATE(ctx->Polygon.SmoothFlag, attr->Polygon.SmoothFlag, GL_POLYGON_SMOOTH);
962       TEST_AND_UPDATE(ctx->Polygon.StippleFlag, attr->Polygon.StippleFlag, GL_POLYGON_STIPPLE);
963       TEST_AND_UPDATE(ctx->Polygon.CullFlag, attr->Polygon.CullFlag, GL_CULL_FACE);
964       TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, attr->Polygon.OffsetPoint,
965                       GL_POLYGON_OFFSET_POINT);
966       TEST_AND_UPDATE(ctx->Polygon.OffsetLine, attr->Polygon.OffsetLine,
967                       GL_POLYGON_OFFSET_LINE);
968       TEST_AND_UPDATE(ctx->Polygon.OffsetFill, attr->Polygon.OffsetFill,
969                       GL_POLYGON_OFFSET_FILL);
970    }
971 
972    if (mask & GL_POLYGON_STIPPLE_BIT) {
973       memcpy(ctx->PolygonStipple, attr->PolygonStipple, 32*sizeof(GLuint));
974 
975       ctx->NewDriverState |= ST_NEW_POLY_STIPPLE;
976    }
977 
978    if (mask & GL_SCISSOR_BIT) {
979       unsigned i;
980 
981       for (i = 0; i < ctx->Const.MaxViewports; i++) {
982          _mesa_set_scissor(ctx, i,
983                            attr->Scissor.ScissorArray[i].X,
984                            attr->Scissor.ScissorArray[i].Y,
985                            attr->Scissor.ScissorArray[i].Width,
986                            attr->Scissor.ScissorArray[i].Height);
987          TEST_AND_UPDATE_INDEX(ctx->Scissor.EnableFlags,
988                                attr->Scissor.EnableFlags, i, GL_SCISSOR_TEST);
989       }
990       if (ctx->Extensions.EXT_window_rectangles) {
991          STATIC_ASSERT(sizeof(struct gl_scissor_rect) ==
992                        4 * sizeof(GLint));
993          _mesa_WindowRectanglesEXT(
994                attr->Scissor.WindowRectMode, attr->Scissor.NumWindowRects,
995                (const GLint *)attr->Scissor.WindowRects);
996       }
997    }
998 
999    if (mask & GL_STENCIL_BUFFER_BIT) {
1000       TEST_AND_UPDATE(ctx->Stencil.Enabled, attr->Stencil.Enabled,
1001                       GL_STENCIL_TEST);
1002       TEST_AND_CALL1(Stencil.Clear, ClearStencil);
1003       if (ctx->Extensions.EXT_stencil_two_side) {
1004          TEST_AND_UPDATE(ctx->Stencil.TestTwoSide, attr->Stencil.TestTwoSide,
1005                          GL_STENCIL_TEST_TWO_SIDE_EXT);
1006          _mesa_ActiveStencilFaceEXT(attr->Stencil.ActiveFace
1007                                     ? GL_BACK : GL_FRONT);
1008       }
1009       /* front state */
1010       _mesa_StencilFuncSeparate(GL_FRONT,
1011                                 attr->Stencil.Function[0],
1012                                 attr->Stencil.Ref[0],
1013                                 attr->Stencil.ValueMask[0]);
1014       TEST_AND_CALL1_SEL(Stencil.WriteMask[0], StencilMaskSeparate, GL_FRONT);
1015       _mesa_StencilOpSeparate(GL_FRONT, attr->Stencil.FailFunc[0],
1016                               attr->Stencil.ZFailFunc[0],
1017                               attr->Stencil.ZPassFunc[0]);
1018       /* back state */
1019       _mesa_StencilFuncSeparate(GL_BACK,
1020                                 attr->Stencil.Function[1],
1021                                 attr->Stencil.Ref[1],
1022                                 attr->Stencil.ValueMask[1]);
1023       TEST_AND_CALL1_SEL(Stencil.WriteMask[1], StencilMaskSeparate, GL_BACK);
1024       _mesa_StencilOpSeparate(GL_BACK, attr->Stencil.FailFunc[1],
1025                               attr->Stencil.ZFailFunc[1],
1026                               attr->Stencil.ZPassFunc[1]);
1027    }
1028 
1029    if (mask & GL_TRANSFORM_BIT) {
1030       GLuint i;
1031       TEST_AND_CALL1(Transform.MatrixMode, MatrixMode);
1032       if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
1033          _math_matrix_analyse(ctx->ProjectionMatrixStack.Top);
1034 
1035       ctx->NewState |= _NEW_TRANSFORM;
1036       ctx->NewDriverState |= ST_NEW_CLIP_STATE;
1037 
1038       /* restore clip planes */
1039       for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
1040          const GLfloat *eyePlane = attr->Transform.EyeUserPlane[i];
1041          COPY_4V(ctx->Transform.EyeUserPlane[i], eyePlane);
1042          TEST_AND_UPDATE_BIT(ctx->Transform.ClipPlanesEnabled,
1043                              attr->Transform.ClipPlanesEnabled, i,
1044                              GL_CLIP_PLANE0 + i);
1045       }
1046 
1047       /* normalize/rescale */
1048       TEST_AND_UPDATE(ctx->Transform.Normalize, attr->Transform.Normalize,
1049                       GL_NORMALIZE);
1050       TEST_AND_UPDATE(ctx->Transform.RescaleNormals,
1051                       attr->Transform.RescaleNormals, GL_RESCALE_NORMAL_EXT);
1052 
1053       if (!ctx->Extensions.AMD_depth_clamp_separate) {
1054          TEST_AND_UPDATE(ctx->Transform.DepthClampNear &&
1055                          ctx->Transform.DepthClampFar,
1056                          attr->Transform.DepthClampNear &&
1057                          attr->Transform.DepthClampFar, GL_DEPTH_CLAMP);
1058       } else {
1059          TEST_AND_UPDATE(ctx->Transform.DepthClampNear,
1060                          attr->Transform.DepthClampNear,
1061                          GL_DEPTH_CLAMP_NEAR_AMD);
1062          TEST_AND_UPDATE(ctx->Transform.DepthClampFar,
1063                          attr->Transform.DepthClampFar,
1064                          GL_DEPTH_CLAMP_FAR_AMD);
1065       }
1066 
1067       if (ctx->Extensions.ARB_clip_control) {
1068          TEST_AND_CALL2(Transform.ClipOrigin, Transform.ClipDepthMode,
1069                         ClipControl);
1070       }
1071    }
1072 
1073    if (mask & GL_TEXTURE_BIT) {
1074       pop_texture_group(ctx, &attr->Texture);
1075       ctx->NewState |= _NEW_TEXTURE_OBJECT | _NEW_TEXTURE_STATE |
1076                        _NEW_FF_VERT_PROGRAM | _NEW_FF_FRAG_PROGRAM;
1077    }
1078 
1079    if (mask & GL_VIEWPORT_BIT) {
1080       unsigned i;
1081 
1082       for (i = 0; i < ctx->Const.MaxViewports; i++) {
1083          const struct gl_viewport_attrib *vp = &attr->Viewport.ViewportArray[i];
1084 
1085          if (memcmp(&ctx->ViewportArray[i].X, &vp->X, sizeof(float) * 6)) {
1086             ctx->NewState |= _NEW_VIEWPORT;
1087             ctx->NewDriverState |= ST_NEW_VIEWPORT;
1088 
1089             memcpy(&ctx->ViewportArray[i].X, &vp->X, sizeof(float) * 6);
1090 
1091             if (ctx->invalidate_on_gl_viewport)
1092                st_manager_invalidate_drawables(ctx);
1093          }
1094       }
1095 
1096       if (ctx->Extensions.NV_conservative_raster) {
1097          GLuint biasx = attr->Viewport.SubpixelPrecisionBias[0];
1098          GLuint biasy = attr->Viewport.SubpixelPrecisionBias[1];
1099          _mesa_SubpixelPrecisionBiasNV(biasx, biasy);
1100       }
1101    }
1102 
1103    if (mask & GL_MULTISAMPLE_BIT_ARB) {
1104       TEST_AND_UPDATE(ctx->Multisample.Enabled,
1105                       attr->Multisample.Enabled,
1106                       GL_MULTISAMPLE);
1107 
1108       TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
1109                       attr->Multisample.SampleCoverage,
1110                       GL_SAMPLE_COVERAGE);
1111 
1112       TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
1113                       attr->Multisample.SampleAlphaToCoverage,
1114                       GL_SAMPLE_ALPHA_TO_COVERAGE);
1115 
1116       TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
1117                       attr->Multisample.SampleAlphaToOne,
1118                       GL_SAMPLE_ALPHA_TO_ONE);
1119 
1120       TEST_AND_CALL2(Multisample.SampleCoverageValue,
1121                      Multisample.SampleCoverageInvert, SampleCoverage);
1122 
1123       TEST_AND_CALL1(Multisample.SampleAlphaToCoverageDitherControl,
1124                      AlphaToCoverageDitherControlNV);
1125    }
1126 
1127    ctx->PopAttribState = attr->OldPopAttribStateMask;
1128 }
1129 
1130 
1131 /**
1132  * Copy gl_pixelstore_attrib from src to dst, updating buffer
1133  * object refcounts.
1134  */
1135 static void
copy_pixelstore(struct gl_context * ctx,struct gl_pixelstore_attrib * dst,const struct gl_pixelstore_attrib * src)1136 copy_pixelstore(struct gl_context *ctx,
1137                 struct gl_pixelstore_attrib *dst,
1138                 const struct gl_pixelstore_attrib *src)
1139 {
1140    dst->Alignment = src->Alignment;
1141    dst->RowLength = src->RowLength;
1142    dst->SkipPixels = src->SkipPixels;
1143    dst->SkipRows = src->SkipRows;
1144    dst->ImageHeight = src->ImageHeight;
1145    dst->SkipImages = src->SkipImages;
1146    dst->SwapBytes = src->SwapBytes;
1147    dst->LsbFirst = src->LsbFirst;
1148    dst->Invert = src->Invert;
1149    _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
1150 }
1151 
1152 
1153 #define GL_CLIENT_PACK_BIT (1<<20)
1154 #define GL_CLIENT_UNPACK_BIT (1<<21)
1155 
1156 static void
copy_vertex_attrib_array(struct gl_context * ctx,struct gl_array_attributes * dst,const struct gl_array_attributes * src)1157 copy_vertex_attrib_array(struct gl_context *ctx,
1158                          struct gl_array_attributes *dst,
1159                          const struct gl_array_attributes *src)
1160 {
1161    dst->Ptr            = src->Ptr;
1162    dst->RelativeOffset = src->RelativeOffset;
1163    dst->Format         = src->Format;
1164    dst->Stride         = src->Stride;
1165    dst->BufferBindingIndex = src->BufferBindingIndex;
1166    dst->_EffBufferBindingIndex = src->_EffBufferBindingIndex;
1167    dst->_EffRelativeOffset = src->_EffRelativeOffset;
1168 }
1169 
1170 static void
copy_vertex_buffer_binding(struct gl_context * ctx,struct gl_vertex_buffer_binding * dst,const struct gl_vertex_buffer_binding * src)1171 copy_vertex_buffer_binding(struct gl_context *ctx,
1172                            struct gl_vertex_buffer_binding *dst,
1173                            const struct gl_vertex_buffer_binding *src)
1174 {
1175    dst->Offset          = src->Offset;
1176    dst->Stride          = src->Stride;
1177    dst->InstanceDivisor = src->InstanceDivisor;
1178    dst->_BoundArrays    = src->_BoundArrays;
1179    dst->_EffBoundArrays = src->_EffBoundArrays;
1180    dst->_EffOffset      = src->_EffOffset;
1181 
1182    _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
1183 }
1184 
1185 /**
1186  * Copy gl_vertex_array_object from src to dest.
1187  * 'dest' must be in an initialized state.
1188  */
1189 static void
copy_array_object(struct gl_context * ctx,struct gl_vertex_array_object * dest,struct gl_vertex_array_object * src,unsigned copy_attrib_mask)1190 copy_array_object(struct gl_context *ctx,
1191                   struct gl_vertex_array_object *dest,
1192                   struct gl_vertex_array_object *src,
1193                   unsigned copy_attrib_mask)
1194 {
1195    /* skip Name */
1196    /* skip RefCount */
1197 
1198    while (copy_attrib_mask) {
1199       unsigned i = u_bit_scan(&copy_attrib_mask);
1200 
1201       copy_vertex_attrib_array(ctx, &dest->VertexAttrib[i], &src->VertexAttrib[i]);
1202       copy_vertex_buffer_binding(ctx, &dest->BufferBinding[i], &src->BufferBinding[i]);
1203    }
1204 
1205    /* Enabled must be the same than on push */
1206    dest->Enabled = src->Enabled;
1207    dest->_EnabledWithMapMode = src->_EnabledWithMapMode;
1208    /* The bitmask of bound VBOs needs to match the VertexBinding array */
1209    dest->VertexAttribBufferMask = src->VertexAttribBufferMask;
1210    dest->NonZeroDivisorMask = src->NonZeroDivisorMask;
1211    dest->NonIdentityBufferAttribMapping = src->NonIdentityBufferAttribMapping;
1212    dest->_AttributeMapMode = src->_AttributeMapMode;
1213    /* skip NumUpdates and IsDynamic because they can only increase, not decrease */
1214 }
1215 
1216 /**
1217  * Copy gl_array_attrib from src to dest.
1218  * 'dest' must be in an initialized state.
1219  */
1220 static void
copy_array_attrib(struct gl_context * ctx,struct gl_array_attrib * dest,struct gl_array_attrib * src,bool vbo_deleted,unsigned copy_attrib_mask)1221 copy_array_attrib(struct gl_context *ctx,
1222                   struct gl_array_attrib *dest,
1223                   struct gl_array_attrib *src,
1224                   bool vbo_deleted,
1225                   unsigned copy_attrib_mask)
1226 {
1227    /* skip ArrayObj */
1228    /* skip DefaultArrayObj, Objects */
1229    dest->ActiveTexture = src->ActiveTexture;
1230    dest->LockFirst = src->LockFirst;
1231    dest->LockCount = src->LockCount;
1232    dest->PrimitiveRestart = src->PrimitiveRestart;
1233    dest->PrimitiveRestartFixedIndex = src->PrimitiveRestartFixedIndex;
1234    dest->RestartIndex = src->RestartIndex;
1235    memcpy(dest->_PrimitiveRestart, src->_PrimitiveRestart,
1236           sizeof(src->_PrimitiveRestart));
1237    memcpy(dest->_RestartIndex, src->_RestartIndex, sizeof(src->_RestartIndex));
1238    /* skip NewState */
1239    /* skip RebindArrays */
1240 
1241    if (!vbo_deleted)
1242       copy_array_object(ctx, dest->VAO, src->VAO, copy_attrib_mask);
1243 
1244    /* skip ArrayBufferObj */
1245    /* skip IndexBufferObj */
1246 }
1247 
1248 /**
1249  * Save the content of src to dest.
1250  */
1251 static void
save_array_attrib(struct gl_context * ctx,struct gl_array_attrib * dest,struct gl_array_attrib * src)1252 save_array_attrib(struct gl_context *ctx,
1253                   struct gl_array_attrib *dest,
1254                   struct gl_array_attrib *src)
1255 {
1256    /* Set the Name, needed for restore, but do never overwrite.
1257     * Needs to match value in the object hash. */
1258    dest->VAO->Name = src->VAO->Name;
1259    dest->VAO->NonDefaultStateMask = src->VAO->NonDefaultStateMask;
1260    /* And copy all of the rest. */
1261    copy_array_attrib(ctx, dest, src, false, src->VAO->NonDefaultStateMask);
1262 
1263    /* Just reference them here */
1264    _mesa_reference_buffer_object(ctx, &dest->ArrayBufferObj,
1265                                  src->ArrayBufferObj);
1266    _mesa_reference_buffer_object(ctx, &dest->VAO->IndexBufferObj,
1267                                  src->VAO->IndexBufferObj);
1268 }
1269 
1270 /**
1271  * Restore the content of src to dest.
1272  */
1273 static void
restore_array_attrib(struct gl_context * ctx,struct gl_array_attrib * dest,struct gl_array_attrib * src)1274 restore_array_attrib(struct gl_context *ctx,
1275                      struct gl_array_attrib *dest,
1276                      struct gl_array_attrib *src)
1277 {
1278    bool is_vao_name_zero = src->VAO->Name == 0;
1279 
1280    /* The ARB_vertex_array_object spec says:
1281     *
1282     *     "BindVertexArray fails and an INVALID_OPERATION error is generated
1283     *     if array is not a name returned from a previous call to
1284     *     GenVertexArrays, or if such a name has since been deleted with
1285     *     DeleteVertexArrays."
1286     *
1287     * Therefore popping a deleted VAO cannot magically recreate it.
1288     */
1289    if (!is_vao_name_zero && !_mesa_IsVertexArray(src->VAO->Name))
1290       return;
1291 
1292    _mesa_BindVertexArray(src->VAO->Name);
1293 
1294    /* Restore or recreate the buffer objects by the names ... */
1295    if (is_vao_name_zero || !src->ArrayBufferObj ||
1296        _mesa_IsBuffer(src->ArrayBufferObj->Name)) {
1297       /* ... and restore its content */
1298       dest->VAO->NonDefaultStateMask |= src->VAO->NonDefaultStateMask;
1299       copy_array_attrib(ctx, dest, src, false,
1300                         dest->VAO->NonDefaultStateMask);
1301 
1302       _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB,
1303                        src->ArrayBufferObj ?
1304                           src->ArrayBufferObj->Name : 0);
1305    } else {
1306       copy_array_attrib(ctx, dest, src, true, 0);
1307    }
1308 
1309    if (is_vao_name_zero || !src->VAO->IndexBufferObj ||
1310        _mesa_IsBuffer(src->VAO->IndexBufferObj->Name)) {
1311       _mesa_BindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,
1312                        src->VAO->IndexBufferObj ?
1313                           src->VAO->IndexBufferObj->Name : 0);
1314    }
1315 
1316    _mesa_update_edgeflag_state_vao(ctx);
1317    _mesa_set_varying_vp_inputs(ctx, ctx->VertexProgram._VPModeInputFilter &
1318                                ctx->Array.VAO->_EnabledWithMapMode);
1319 }
1320 
1321 
1322 void GLAPIENTRY
_mesa_PushClientAttrib(GLbitfield mask)1323 _mesa_PushClientAttrib(GLbitfield mask)
1324 {
1325    struct gl_client_attrib_node *head;
1326 
1327    GET_CURRENT_CONTEXT(ctx);
1328 
1329    if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
1330       _mesa_error(ctx, GL_STACK_OVERFLOW, "glPushClientAttrib");
1331       return;
1332    }
1333 
1334    head = &ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
1335    head->Mask = mask;
1336 
1337    if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
1338       copy_pixelstore(ctx, &head->Pack, &ctx->Pack);
1339       copy_pixelstore(ctx, &head->Unpack, &ctx->Unpack);
1340    }
1341 
1342    if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1343       _mesa_initialize_vao(ctx, &head->VAO, 0);
1344       /* Use the VAO declared within the node instead of allocating it. */
1345       head->Array.VAO = &head->VAO;
1346       save_array_attrib(ctx, &head->Array, &ctx->Array);
1347    }
1348 
1349    ctx->ClientAttribStackDepth++;
1350 }
1351 
1352 
1353 void GLAPIENTRY
_mesa_PopClientAttrib(void)1354 _mesa_PopClientAttrib(void)
1355 {
1356    struct gl_client_attrib_node *head;
1357 
1358    GET_CURRENT_CONTEXT(ctx);
1359 
1360    if (ctx->ClientAttribStackDepth == 0) {
1361       _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib");
1362       return;
1363    }
1364 
1365    ctx->ClientAttribStackDepth--;
1366    head = &ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
1367 
1368    if (head->Mask & GL_CLIENT_PIXEL_STORE_BIT) {
1369       copy_pixelstore(ctx, &ctx->Pack, &head->Pack);
1370       _mesa_reference_buffer_object(ctx, &head->Pack.BufferObj, NULL);
1371 
1372       copy_pixelstore(ctx, &ctx->Unpack, &head->Unpack);
1373       _mesa_reference_buffer_object(ctx, &head->Unpack.BufferObj, NULL);
1374    }
1375 
1376    if (head->Mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1377       restore_array_attrib(ctx, &ctx->Array, &head->Array);
1378 
1379       /* _mesa_unbind_array_object_vbos can't use NonDefaultStateMask because
1380        * it's used by internal VAOs which don't always update the mask, so do
1381        * it manually here.
1382        */
1383       GLbitfield mask = head->VAO.NonDefaultStateMask;
1384       while (mask) {
1385          unsigned i = u_bit_scan(&mask);
1386          _mesa_reference_buffer_object(ctx, &head->VAO.BufferBinding[i].BufferObj, NULL);
1387       }
1388 
1389       _mesa_reference_buffer_object(ctx, &head->VAO.IndexBufferObj, NULL);
1390       _mesa_reference_buffer_object(ctx, &head->Array.ArrayBufferObj, NULL);
1391    }
1392 }
1393 
1394 void GLAPIENTRY
_mesa_ClientAttribDefaultEXT(GLbitfield mask)1395 _mesa_ClientAttribDefaultEXT( GLbitfield mask )
1396 {
1397    if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
1398       _mesa_PixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
1399       _mesa_PixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
1400       _mesa_PixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
1401       _mesa_PixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
1402       _mesa_PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1403       _mesa_PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
1404       _mesa_PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
1405       _mesa_PixelStorei(GL_UNPACK_ALIGNMENT, 4);
1406       _mesa_PixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
1407       _mesa_PixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
1408       _mesa_PixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
1409       _mesa_PixelStorei(GL_PACK_SKIP_IMAGES, 0);
1410       _mesa_PixelStorei(GL_PACK_ROW_LENGTH, 0);
1411       _mesa_PixelStorei(GL_PACK_SKIP_ROWS, 0);
1412       _mesa_PixelStorei(GL_PACK_SKIP_PIXELS, 0);
1413       _mesa_PixelStorei(GL_PACK_ALIGNMENT, 4);
1414 
1415       _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1416       _mesa_BindBuffer(GL_PIXEL_PACK_BUFFER, 0);
1417    }
1418    if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1419       GET_CURRENT_CONTEXT(ctx);
1420       int i;
1421 
1422       _mesa_BindBuffer(GL_ARRAY_BUFFER, 0);
1423       _mesa_BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1424 
1425       _mesa_DisableClientState(GL_EDGE_FLAG_ARRAY);
1426       _mesa_EdgeFlagPointer(0, 0);
1427 
1428       _mesa_DisableClientState(GL_INDEX_ARRAY);
1429       _mesa_IndexPointer(GL_FLOAT, 0, 0);
1430 
1431       _mesa_DisableClientState(GL_SECONDARY_COLOR_ARRAY);
1432       _mesa_SecondaryColorPointer(4, GL_FLOAT, 0, 0);
1433 
1434       _mesa_DisableClientState(GL_FOG_COORD_ARRAY);
1435       _mesa_FogCoordPointer(GL_FLOAT, 0, 0);
1436 
1437       for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
1438          _mesa_ClientActiveTexture(GL_TEXTURE0 + i);
1439          _mesa_DisableClientState(GL_TEXTURE_COORD_ARRAY);
1440          _mesa_TexCoordPointer(4, GL_FLOAT, 0, 0);
1441       }
1442 
1443       _mesa_DisableClientState(GL_COLOR_ARRAY);
1444       _mesa_ColorPointer(4, GL_FLOAT, 0, 0);
1445 
1446       _mesa_DisableClientState(GL_NORMAL_ARRAY);
1447       _mesa_NormalPointer(GL_FLOAT, 0, 0);
1448 
1449       _mesa_DisableClientState(GL_VERTEX_ARRAY);
1450       _mesa_VertexPointer(4, GL_FLOAT, 0, 0);
1451 
1452       for (i = 0; i < ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs; i++) {
1453          _mesa_DisableVertexAttribArray(i);
1454          _mesa_VertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, 0, 0);
1455       }
1456 
1457       _mesa_ClientActiveTexture(GL_TEXTURE0);
1458 
1459       _mesa_PrimitiveRestartIndex_no_error(0);
1460       if (ctx->Version >= 31)
1461          _mesa_Disable(GL_PRIMITIVE_RESTART);
1462       else if (_mesa_has_NV_primitive_restart(ctx))
1463          _mesa_DisableClientState(GL_PRIMITIVE_RESTART_NV);
1464 
1465       if (_mesa_has_ARB_ES3_compatibility(ctx))
1466          _mesa_Disable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
1467    }
1468 }
1469 
1470 void GLAPIENTRY
_mesa_PushClientAttribDefaultEXT(GLbitfield mask)1471 _mesa_PushClientAttribDefaultEXT( GLbitfield mask )
1472 {
1473    _mesa_PushClientAttrib(mask);
1474    _mesa_ClientAttribDefaultEXT(mask);
1475 }
1476 
1477 
1478 /**
1479  * Free any attribute state data that might be attached to the context.
1480  */
1481 void
_mesa_free_attrib_data(struct gl_context * ctx)1482 _mesa_free_attrib_data(struct gl_context *ctx)
1483 {
1484    for (unsigned i = 0; i < ARRAY_SIZE(ctx->AttribStack); i++)
1485       FREE(ctx->AttribStack[i]);
1486 }
1487 
1488 
1489 void
_mesa_init_attrib(struct gl_context * ctx)1490 _mesa_init_attrib(struct gl_context *ctx)
1491 {
1492    /* Renderer and client attribute stacks */
1493    ctx->AttribStackDepth = 0;
1494    ctx->ClientAttribStackDepth = 0;
1495 }
1496