• 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 "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    if (ctx->Extensions.ARB_point_sprite) {
427       TEST_AND_UPDATE(ctx->Point.PointSprite, enable->PointSprite,
428                       GL_POINT_SPRITE);
429    }
430    TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, enable->PolygonOffsetPoint,
431                    GL_POLYGON_OFFSET_POINT);
432    TEST_AND_UPDATE(ctx->Polygon.OffsetLine, enable->PolygonOffsetLine,
433                    GL_POLYGON_OFFSET_LINE);
434    TEST_AND_UPDATE(ctx->Polygon.OffsetFill, enable->PolygonOffsetFill,
435                    GL_POLYGON_OFFSET_FILL);
436    TEST_AND_UPDATE(ctx->Polygon.SmoothFlag, enable->PolygonSmooth,
437                    GL_POLYGON_SMOOTH);
438    TEST_AND_UPDATE(ctx->Polygon.StippleFlag, enable->PolygonStipple,
439                    GL_POLYGON_STIPPLE);
440    if (ctx->Scissor.EnableFlags != enable->Scissor) {
441       unsigned i;
442 
443       for (i = 0; i < ctx->Const.MaxViewports; i++) {
444          TEST_AND_UPDATE_INDEX(ctx->Scissor.EnableFlags, enable->Scissor,
445                                i, GL_SCISSOR_TEST);
446       }
447    }
448    TEST_AND_UPDATE(ctx->Stencil.Enabled, enable->Stencil, GL_STENCIL_TEST);
449    if (ctx->Extensions.EXT_stencil_two_side) {
450       TEST_AND_UPDATE(ctx->Stencil.TestTwoSide, enable->StencilTwoSide,
451                       GL_STENCIL_TEST_TWO_SIDE_EXT);
452    }
453    TEST_AND_UPDATE(ctx->Multisample.Enabled, enable->MultisampleEnabled,
454                    GL_MULTISAMPLE_ARB);
455    TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
456                    enable->SampleAlphaToCoverage,
457                    GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
458    TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
459                    enable->SampleAlphaToOne,
460                    GL_SAMPLE_ALPHA_TO_ONE_ARB);
461    TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
462                    enable->SampleCoverage,
463                    GL_SAMPLE_COVERAGE_ARB);
464    /* GL_ARB_vertex_program */
465    TEST_AND_UPDATE(ctx->VertexProgram.Enabled,
466                    enable->VertexProgram,
467                    GL_VERTEX_PROGRAM_ARB);
468    TEST_AND_UPDATE(ctx->VertexProgram.PointSizeEnabled,
469                    enable->VertexProgramPointSize,
470                    GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
471    TEST_AND_UPDATE(ctx->VertexProgram.TwoSideEnabled,
472                    enable->VertexProgramTwoSide,
473                    GL_VERTEX_PROGRAM_TWO_SIDE_ARB);
474 
475    /* GL_ARB_fragment_program */
476    TEST_AND_UPDATE(ctx->FragmentProgram.Enabled,
477                    enable->FragmentProgram,
478                    GL_FRAGMENT_PROGRAM_ARB);
479 
480    /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
481    TEST_AND_UPDATE(ctx->Color.sRGBEnabled, enable->sRGBEnabled,
482                    GL_FRAMEBUFFER_SRGB);
483 
484    /* GL_NV_conservative_raster */
485    if (ctx->Extensions.NV_conservative_raster) {
486       TEST_AND_UPDATE(ctx->ConservativeRasterization,
487                       enable->ConservativeRasterization,
488                       GL_CONSERVATIVE_RASTERIZATION_NV);
489    }
490 
491    const unsigned curTexUnitSave = ctx->Texture.CurrentUnit;
492 
493    /* texture unit enables */
494    for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
495       const GLbitfield enabled = enable->Texture[i];
496       const GLbitfield gen_enabled = enable->TexGen[i];
497       const struct gl_fixedfunc_texture_unit *unit = &ctx->Texture.FixedFuncUnit[i];
498       const GLbitfield old_enabled = unit->Enabled;
499       const GLbitfield old_gen_enabled = unit->TexGenEnabled;
500 
501       if (old_enabled == enabled && old_gen_enabled == gen_enabled)
502          continue;
503 
504       ctx->Texture.CurrentUnit = i;
505 
506       if (old_enabled != enabled) {
507          TEST_AND_UPDATE_BIT(old_enabled, enabled, TEXTURE_1D_INDEX, GL_TEXTURE_1D);
508          TEST_AND_UPDATE_BIT(old_enabled, enabled, TEXTURE_2D_INDEX, GL_TEXTURE_2D);
509          TEST_AND_UPDATE_BIT(old_enabled, enabled, TEXTURE_3D_INDEX, GL_TEXTURE_3D);
510          if (ctx->Extensions.NV_texture_rectangle) {
511             TEST_AND_UPDATE_BIT(old_enabled, enabled, TEXTURE_RECT_INDEX,
512                                 GL_TEXTURE_RECTANGLE);
513          }
514          TEST_AND_UPDATE_BIT(old_enabled, enabled, TEXTURE_CUBE_INDEX,
515                              GL_TEXTURE_CUBE_MAP);
516       }
517 
518       if (old_gen_enabled != gen_enabled) {
519          TEST_AND_UPDATE_BIT(old_gen_enabled, gen_enabled, 0, GL_TEXTURE_GEN_S);
520          TEST_AND_UPDATE_BIT(old_gen_enabled, gen_enabled, 1, GL_TEXTURE_GEN_T);
521          TEST_AND_UPDATE_BIT(old_gen_enabled, gen_enabled, 2, GL_TEXTURE_GEN_R);
522          TEST_AND_UPDATE_BIT(old_gen_enabled, gen_enabled, 3, GL_TEXTURE_GEN_Q);
523       }
524    }
525 
526    ctx->Texture.CurrentUnit = curTexUnitSave;
527 }
528 
529 
530 /**
531  * Pop/restore texture attribute/group state.
532  */
533 static void
pop_texture_group(struct gl_context * ctx,struct gl_texture_attrib_node * texstate)534 pop_texture_group(struct gl_context *ctx, struct gl_texture_attrib_node *texstate)
535 {
536    GLuint u;
537 
538    _mesa_lock_context_textures(ctx);
539 
540    /* Restore fixed-function texture unit states. */
541    for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
542       const struct gl_fixedfunc_texture_unit *unit =
543          &texstate->FixedFuncUnit[u];
544       struct gl_fixedfunc_texture_unit *destUnit =
545          &ctx->Texture.FixedFuncUnit[u];
546 
547       ctx->Texture.CurrentUnit = u;
548 
549       /* Fast path for other drivers. */
550       memcpy(destUnit, unit, sizeof(*unit));
551       destUnit->_CurrentCombine = NULL;
552       ctx->Texture.Unit[u].LodBias = texstate->LodBias[u];
553       ctx->Texture.Unit[u].LodBiasQuantized = texstate->LodBiasQuantized[u];
554    }
555 
556    /* Restore saved textures. */
557    unsigned num_tex_saved = texstate->NumTexSaved;
558    for (u = 0; u < num_tex_saved; u++) {
559       gl_texture_index tgt;
560 
561       ctx->Texture.CurrentUnit = u;
562 
563       /* Restore texture object state for each target */
564       for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
565          const struct gl_texture_object *savedObj = &texstate->SavedObj[u][tgt];
566          struct gl_texture_object *texObj =
567             _mesa_get_tex_unit(ctx, u)->CurrentTex[tgt];
568          bool is_msaa = tgt == TEXTURE_2D_MULTISAMPLE_INDEX ||
569                         tgt == TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX;
570 
571          /* According to the OpenGL 4.6 Compatibility Profile specification,
572           * table 23.17, GL_TEXTURE_BINDING_2D_MULTISAMPLE and
573           * GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY do not belong in the
574           * texture attrib group.
575           */
576          if (!is_msaa && texObj->Name != savedObj->Name) {
577             /* We don't need to check whether the texture target is supported,
578              * because we wouldn't get in this conditional block if it wasn't.
579              */
580             _mesa_BindTexture_no_error(texObj->Target, savedObj->Name);
581             texObj = _mesa_get_tex_unit(ctx, u)->CurrentTex[tgt];
582          }
583 
584          /* Default texture object states are restored separately below. */
585          if (texObj->Name == 0)
586             continue;
587 
588          /* But in the MSAA case, where the currently-bound object is not the
589           * default state, we should still restore the saved default object's
590           * data when that's what was saved initially.
591           */
592          if (savedObj->Name == 0)
593             savedObj = &texstate->SavedDefaultObj[tgt];
594 
595          if (!copy_texture_attribs(texObj, savedObj, tgt))
596             continue;
597 
598          st_texture_release_all_sampler_views(st_context(ctx), texObj);
599       }
600    }
601 
602    /* Restore textures in units that were not used before glPushAttrib (thus
603     * they were not saved) but were used after glPushAttrib. Revert
604     * the bindings to Name = 0.
605     */
606    unsigned num_tex_changed = ctx->Texture.NumCurrentTexUsed;
607    for (u = num_tex_saved; u < num_tex_changed; u++) {
608       ctx->Texture.CurrentUnit = u;
609 
610       for (gl_texture_index tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
611          struct gl_texture_object *texObj =
612             _mesa_get_tex_unit(ctx, u)->CurrentTex[tgt];
613          bool is_msaa = tgt == TEXTURE_2D_MULTISAMPLE_INDEX ||
614                         tgt == TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX;
615 
616          /* According to the OpenGL 4.6 Compatibility Profile specification,
617           * table 23.17, GL_TEXTURE_BINDING_2D_MULTISAMPLE and
618           * GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY do not belong in the
619           * texture attrib group.
620           */
621          if (!is_msaa && texObj->Name != 0) {
622             /* We don't need to check whether the texture target is supported,
623              * because we wouldn't get in this conditional block if it wasn't.
624              */
625             _mesa_BindTexture_no_error(texObj->Target, 0);
626          }
627       }
628    }
629 
630    /* Restore default texture object states. */
631    for (gl_texture_index tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
632       struct gl_texture_object *dst = ctx->Shared->DefaultTex[tex];
633       const struct gl_texture_object *src = &texstate->SavedDefaultObj[tex];
634 
635       copy_texture_attribs(dst, src, tex);
636    }
637 
638    _mesa_ActiveTexture(GL_TEXTURE0_ARB + texstate->CurrentUnit);
639    _mesa_unlock_context_textures(ctx);
640 }
641 
642 
643 #define TEST_AND_CALL1(FIELD, CALL) do { \
644       if (ctx->FIELD != attr->FIELD)     \
645          _mesa_##CALL(attr->FIELD);      \
646    } while (0)
647 
648 #define TEST_AND_CALL1_SEL(FIELD, CALL, SEL) do { \
649       if (ctx->FIELD != attr->FIELD)              \
650          _mesa_##CALL(SEL, attr->FIELD);          \
651    } while (0)
652 
653 #define TEST_AND_CALL2(FIELD1, FIELD2, CALL) do {                     \
654       if (ctx->FIELD1 != attr->FIELD1 || ctx->FIELD2 != attr->FIELD2) \
655          _mesa_##CALL(attr->FIELD1, attr->FIELD2);                    \
656    } while (0)
657 
658 
659 /*
660  * This function is kind of long just because we have to call a lot
661  * of device driver functions to update device driver state.
662  *
663  * XXX As it is now, most of the pop-code calls immediate-mode Mesa functions
664  * in order to restore GL state.  This isn't terribly efficient but it
665  * ensures that dirty flags and any derived state gets updated correctly.
666  * We could at least check if the value to restore equals the current value
667  * and then skip the Mesa call.
668  */
669 void GLAPIENTRY
_mesa_PopAttrib(void)670 _mesa_PopAttrib(void)
671 {
672    struct gl_attrib_node *attr;
673    GET_CURRENT_CONTEXT(ctx);
674    FLUSH_VERTICES(ctx, 0, 0);
675 
676    if (ctx->AttribStackDepth == 0) {
677       _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopAttrib");
678       return;
679    }
680 
681    ctx->AttribStackDepth--;
682    attr = ctx->AttribStack[ctx->AttribStackDepth];
683 
684    unsigned mask = attr->Mask;
685 
686    /* Flush current attribs. This must be done before PopAttribState is
687     * applied.
688     */
689    if (mask & GL_CURRENT_BIT)
690       FLUSH_CURRENT(ctx, 0);
691 
692    /* Only restore states that have been changed since glPushAttrib. */
693    mask &= ctx->PopAttribState;
694 
695    if (mask & GL_ACCUM_BUFFER_BIT) {
696       _mesa_ClearAccum(attr->Accum.ClearColor[0],
697                        attr->Accum.ClearColor[1],
698                        attr->Accum.ClearColor[2],
699                        attr->Accum.ClearColor[3]);
700    }
701 
702    if (mask & GL_COLOR_BUFFER_BIT) {
703       TEST_AND_CALL1(Color.ClearIndex, ClearIndex);
704       _mesa_ClearColor(attr->Color.ClearColor.f[0],
705                        attr->Color.ClearColor.f[1],
706                        attr->Color.ClearColor.f[2],
707                        attr->Color.ClearColor.f[3]);
708       TEST_AND_CALL1(Color.IndexMask, IndexMask);
709       if (ctx->Color.ColorMask != attr->Color.ColorMask) {
710          if (!ctx->Extensions.EXT_draw_buffers2) {
711             _mesa_ColorMask(GET_COLORMASK_BIT(attr->Color.ColorMask, 0, 0),
712                             GET_COLORMASK_BIT(attr->Color.ColorMask, 0, 1),
713                             GET_COLORMASK_BIT(attr->Color.ColorMask, 0, 2),
714                             GET_COLORMASK_BIT(attr->Color.ColorMask, 0, 3));
715          } else {
716             for (unsigned i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
717                _mesa_ColorMaski(i,
718                                 GET_COLORMASK_BIT(attr->Color.ColorMask, i, 0),
719                                 GET_COLORMASK_BIT(attr->Color.ColorMask, i, 1),
720                                 GET_COLORMASK_BIT(attr->Color.ColorMask, i, 2),
721                                 GET_COLORMASK_BIT(attr->Color.ColorMask, i, 3));
722             }
723          }
724       }
725       if (memcmp(ctx->Color.DrawBuffer, attr->Color.DrawBuffer,
726                  sizeof(attr->Color.DrawBuffer))) {
727          /* Need to determine if more than one color output is
728           * specified.  If so, call glDrawBuffersARB, else call
729           * glDrawBuffer().  This is a subtle, but essential point
730           * since GL_FRONT (for example) is illegal for the former
731           * function, but legal for the later.
732           */
733          GLboolean multipleBuffers = GL_FALSE;
734          GLuint i;
735 
736          for (i = 1; i < ctx->Const.MaxDrawBuffers; i++) {
737             if (attr->Color.DrawBuffer[i] != GL_NONE) {
738                multipleBuffers = GL_TRUE;
739                break;
740             }
741          }
742          /* Call the API_level functions, not _mesa_drawbuffers()
743           * since we need to do error checking on the pop'd
744           * GL_DRAW_BUFFER.
745           * Ex: if GL_FRONT were pushed, but we're popping with a
746           * user FBO bound, GL_FRONT will be illegal and we'll need
747           * to record that error.  Per OpenGL ARB decision.
748           */
749          if (multipleBuffers) {
750             GLenum buffers[MAX_DRAW_BUFFERS];
751 
752             for (unsigned i = 0; i < ctx->Const.MaxDrawBuffers; i++)
753                buffers[i] = attr->Color.DrawBuffer[i];
754 
755             _mesa_DrawBuffers(ctx->Const.MaxDrawBuffers, buffers);
756          } else {
757             _mesa_DrawBuffer(attr->Color.DrawBuffer[0]);
758          }
759       }
760       TEST_AND_UPDATE(ctx->Color.AlphaEnabled, attr->Color.AlphaEnabled,
761                       GL_ALPHA_TEST);
762       TEST_AND_CALL2(Color.AlphaFunc, Color.AlphaRefUnclamped, AlphaFunc);
763       if (ctx->Color.BlendEnabled != attr->Color.BlendEnabled) {
764          if (ctx->Extensions.EXT_draw_buffers2) {
765             for (unsigned i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
766                TEST_AND_UPDATE_INDEX(ctx->Color.BlendEnabled,
767                                      attr->Color.BlendEnabled, i, GL_BLEND);
768             }
769          }
770          else {
771             TEST_AND_UPDATE(ctx->Color.BlendEnabled & 0x1,
772                             attr->Color.BlendEnabled & 0x1, GL_BLEND);
773          }
774       }
775       if (ctx->Color._BlendFuncPerBuffer ||
776           ctx->Color._BlendEquationPerBuffer) {
777          /* set blend per buffer */
778          GLuint buf;
779          for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) {
780             _mesa_BlendFuncSeparateiARB(buf, attr->Color.Blend[buf].SrcRGB,
781                                      attr->Color.Blend[buf].DstRGB,
782                                      attr->Color.Blend[buf].SrcA,
783                                      attr->Color.Blend[buf].DstA);
784             _mesa_BlendEquationSeparateiARB(buf,
785                                          attr->Color.Blend[buf].EquationRGB,
786                                          attr->Color.Blend[buf].EquationA);
787          }
788       }
789       else {
790          /* set same blend modes for all buffers */
791          _mesa_BlendFuncSeparate(attr->Color.Blend[0].SrcRGB,
792                                     attr->Color.Blend[0].DstRGB,
793                                     attr->Color.Blend[0].SrcA,
794                                     attr->Color.Blend[0].DstA);
795          /* This special case is because glBlendEquationSeparateEXT
796           * cannot take GL_LOGIC_OP as a parameter.
797           */
798          if (attr->Color.Blend[0].EquationRGB ==
799              attr->Color.Blend[0].EquationA) {
800             TEST_AND_CALL1(Color.Blend[0].EquationRGB, BlendEquation);
801          }
802          else {
803             TEST_AND_CALL2(Color.Blend[0].EquationRGB,
804                            Color.Blend[0].EquationA, BlendEquationSeparate);
805          }
806       }
807       _mesa_BlendColor(attr->Color.BlendColorUnclamped[0],
808                        attr->Color.BlendColorUnclamped[1],
809                        attr->Color.BlendColorUnclamped[2],
810                        attr->Color.BlendColorUnclamped[3]);
811       TEST_AND_CALL1(Color.LogicOp, LogicOp);
812       TEST_AND_UPDATE(ctx->Color.ColorLogicOpEnabled,
813                       attr->Color.ColorLogicOpEnabled, GL_COLOR_LOGIC_OP);
814       TEST_AND_UPDATE(ctx->Color.IndexLogicOpEnabled,
815                       attr->Color.IndexLogicOpEnabled, GL_INDEX_LOGIC_OP);
816       TEST_AND_UPDATE(ctx->Color.DitherFlag, attr->Color.DitherFlag,
817                       GL_DITHER);
818       if (ctx->Extensions.ARB_color_buffer_float) {
819          TEST_AND_CALL1_SEL(Color.ClampFragmentColor, ClampColor,
820                             GL_CLAMP_FRAGMENT_COLOR);
821       }
822       if (ctx->Extensions.ARB_color_buffer_float || ctx->Version >= 30) {
823          TEST_AND_CALL1_SEL(Color.ClampReadColor, ClampColor,
824                             GL_CLAMP_READ_COLOR);
825       }
826       /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
827       if (ctx->Extensions.EXT_framebuffer_sRGB) {
828          TEST_AND_UPDATE(ctx->Color.sRGBEnabled, attr->Color.sRGBEnabled,
829                          GL_FRAMEBUFFER_SRGB);
830       }
831    }
832 
833    if (mask & GL_CURRENT_BIT) {
834       memcpy(&ctx->Current, &attr->Current,
835              sizeof(struct gl_current_attrib));
836       ctx->NewState |= _NEW_CURRENT_ATTRIB;
837    }
838 
839    if (mask & GL_DEPTH_BUFFER_BIT) {
840       TEST_AND_CALL1(Depth.Func, DepthFunc);
841       TEST_AND_CALL1(Depth.Clear, ClearDepth);
842       TEST_AND_UPDATE(ctx->Depth.Test, attr->Depth.Test, GL_DEPTH_TEST);
843       TEST_AND_CALL1(Depth.Mask, DepthMask);
844       if (ctx->Extensions.EXT_depth_bounds_test) {
845          TEST_AND_UPDATE(ctx->Depth.BoundsTest, attr->Depth.BoundsTest,
846                          GL_DEPTH_BOUNDS_TEST_EXT);
847          TEST_AND_CALL2(Depth.BoundsMin, Depth.BoundsMax, DepthBoundsEXT);
848       }
849    }
850 
851    if (mask & GL_ENABLE_BIT)
852       pop_enable_group(ctx, &attr->Enable);
853 
854    if (mask & GL_EVAL_BIT) {
855       memcpy(&ctx->Eval, &attr->Eval, sizeof(struct gl_eval_attrib));
856       vbo_exec_update_eval_maps(ctx);
857    }
858 
859    if (mask & GL_FOG_BIT) {
860       TEST_AND_UPDATE(ctx->Fog.Enabled, attr->Fog.Enabled, GL_FOG);
861       _mesa_Fogfv(GL_FOG_COLOR, attr->Fog.Color);
862       TEST_AND_CALL1_SEL(Fog.Density, Fogf, GL_FOG_DENSITY);
863       TEST_AND_CALL1_SEL(Fog.Start, Fogf, GL_FOG_START);
864       TEST_AND_CALL1_SEL(Fog.End, Fogf, GL_FOG_END);
865       TEST_AND_CALL1_SEL(Fog.Index, Fogf, GL_FOG_INDEX);
866       TEST_AND_CALL1_SEL(Fog.Mode, Fogi, GL_FOG_MODE);
867    }
868 
869    if (mask & GL_HINT_BIT) {
870       TEST_AND_CALL1_SEL(Hint.PerspectiveCorrection, Hint, GL_PERSPECTIVE_CORRECTION_HINT);
871       TEST_AND_CALL1_SEL(Hint.PointSmooth, Hint, GL_POINT_SMOOTH_HINT);
872       TEST_AND_CALL1_SEL(Hint.LineSmooth, Hint, GL_LINE_SMOOTH_HINT);
873       TEST_AND_CALL1_SEL(Hint.PolygonSmooth, Hint, GL_POLYGON_SMOOTH_HINT);
874       TEST_AND_CALL1_SEL(Hint.Fog, Hint, GL_FOG_HINT);
875       TEST_AND_CALL1_SEL(Hint.TextureCompression, Hint, GL_TEXTURE_COMPRESSION_HINT_ARB);
876    }
877 
878    if (mask & GL_LIGHTING_BIT) {
879       GLuint i;
880       /* lighting enable */
881       TEST_AND_UPDATE(ctx->Light.Enabled, attr->Light.Enabled, GL_LIGHTING);
882       /* per-light state */
883       if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
884          _math_matrix_analyse(ctx->ModelviewMatrixStack.Top);
885 
886       /* Fast path for other drivers. */
887       ctx->NewState |= _NEW_LIGHT_CONSTANTS | _NEW_FF_VERT_PROGRAM;
888 
889       memcpy(ctx->Light.LightSource, attr->Light.LightSource,
890              sizeof(attr->Light.LightSource));
891       memcpy(&ctx->Light.Model, &attr->Light.Model,
892              sizeof(attr->Light.Model));
893 
894       for (i = 0; i < ctx->Const.MaxLights; i++) {
895          TEST_AND_UPDATE(ctx->Light.Light[i].Enabled,
896                          attr->Light.Light[i].Enabled,
897                          GL_LIGHT0 + i);
898          memcpy(&ctx->Light.Light[i], &attr->Light.Light[i],
899                 sizeof(struct gl_light));
900       }
901       /* shade model */
902       TEST_AND_CALL1(Light.ShadeModel, ShadeModel);
903       /* color material */
904       TEST_AND_CALL2(Light.ColorMaterialFace, Light.ColorMaterialMode,
905                      ColorMaterial);
906       TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled,
907                       attr->Light.ColorMaterialEnabled, GL_COLOR_MATERIAL);
908       /* Shininess material is used by the fixed-func vertex program. */
909       ctx->NewState |= _NEW_MATERIAL | _NEW_FF_VERT_PROGRAM;
910       memcpy(&ctx->Light.Material, &attr->Light.Material,
911              sizeof(struct gl_material));
912       if (ctx->Extensions.ARB_color_buffer_float) {
913          TEST_AND_CALL1_SEL(Light.ClampVertexColor, ClampColor, GL_CLAMP_VERTEX_COLOR_ARB);
914       }
915    }
916 
917    if (mask & GL_LINE_BIT) {
918       TEST_AND_UPDATE(ctx->Line.SmoothFlag, attr->Line.SmoothFlag, GL_LINE_SMOOTH);
919       TEST_AND_UPDATE(ctx->Line.StippleFlag, attr->Line.StippleFlag, GL_LINE_STIPPLE);
920       TEST_AND_CALL2(Line.StippleFactor, Line.StipplePattern, LineStipple);
921       TEST_AND_CALL1(Line.Width, LineWidth);
922    }
923 
924    if (mask & GL_LIST_BIT)
925       memcpy(&ctx->List, &attr->List, sizeof(struct gl_list_attrib));
926 
927    if (mask & GL_PIXEL_MODE_BIT) {
928       memcpy(&ctx->Pixel, &attr->Pixel, sizeof(struct gl_pixel_attrib));
929       /* XXX what other pixel state needs to be set by function calls? */
930       _mesa_ReadBuffer(ctx->Pixel.ReadBuffer);
931       ctx->NewState |= _NEW_PIXEL;
932    }
933 
934    if (mask & GL_POINT_BIT) {
935       TEST_AND_CALL1(Point.Size, PointSize);
936       TEST_AND_UPDATE(ctx->Point.SmoothFlag, attr->Point.SmoothFlag, GL_POINT_SMOOTH);
937       _mesa_PointParameterfv(GL_DISTANCE_ATTENUATION_EXT, attr->Point.Params);
938       TEST_AND_CALL1_SEL(Point.MinSize, PointParameterf, GL_POINT_SIZE_MIN_EXT);
939       TEST_AND_CALL1_SEL(Point.MaxSize, PointParameterf, GL_POINT_SIZE_MAX_EXT);
940       TEST_AND_CALL1_SEL(Point.Threshold, PointParameterf, GL_POINT_FADE_THRESHOLD_SIZE_EXT);
941 
942       if (ctx->Extensions.ARB_point_sprite) {
943          if (ctx->Point.CoordReplace != attr->Point.CoordReplace) {
944             ctx->NewState |= _NEW_POINT | _NEW_FF_VERT_PROGRAM;
945             ctx->Point.CoordReplace = attr->Point.CoordReplace;
946          }
947          TEST_AND_UPDATE(ctx->Point.PointSprite, attr->Point.PointSprite,
948                          GL_POINT_SPRITE);
949 
950          if ((ctx->API == API_OPENGL_COMPAT && ctx->Version >= 20)
951              || ctx->API == API_OPENGL_CORE)
952             TEST_AND_CALL1_SEL(Point.SpriteOrigin, PointParameterf, GL_POINT_SPRITE_COORD_ORIGIN);
953       }
954    }
955 
956    if (mask & GL_POLYGON_BIT) {
957       TEST_AND_CALL1(Polygon.CullFaceMode, CullFace);
958       TEST_AND_CALL1(Polygon.FrontFace, FrontFace);
959       TEST_AND_CALL1_SEL(Polygon.FrontMode, PolygonMode, GL_FRONT);
960       TEST_AND_CALL1_SEL(Polygon.BackMode, PolygonMode, GL_BACK);
961       _mesa_polygon_offset_clamp(ctx,
962                                  attr->Polygon.OffsetFactor,
963                                  attr->Polygon.OffsetUnits,
964                                  attr->Polygon.OffsetClamp);
965       TEST_AND_UPDATE(ctx->Polygon.SmoothFlag, attr->Polygon.SmoothFlag, GL_POLYGON_SMOOTH);
966       TEST_AND_UPDATE(ctx->Polygon.StippleFlag, attr->Polygon.StippleFlag, GL_POLYGON_STIPPLE);
967       TEST_AND_UPDATE(ctx->Polygon.CullFlag, attr->Polygon.CullFlag, GL_CULL_FACE);
968       TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, attr->Polygon.OffsetPoint,
969                       GL_POLYGON_OFFSET_POINT);
970       TEST_AND_UPDATE(ctx->Polygon.OffsetLine, attr->Polygon.OffsetLine,
971                       GL_POLYGON_OFFSET_LINE);
972       TEST_AND_UPDATE(ctx->Polygon.OffsetFill, attr->Polygon.OffsetFill,
973                       GL_POLYGON_OFFSET_FILL);
974    }
975 
976    if (mask & GL_POLYGON_STIPPLE_BIT) {
977       memcpy(ctx->PolygonStipple, attr->PolygonStipple, 32*sizeof(GLuint));
978 
979       ctx->NewDriverState |= ST_NEW_POLY_STIPPLE;
980    }
981 
982    if (mask & GL_SCISSOR_BIT) {
983       unsigned i;
984 
985       for (i = 0; i < ctx->Const.MaxViewports; i++) {
986          _mesa_set_scissor(ctx, i,
987                            attr->Scissor.ScissorArray[i].X,
988                            attr->Scissor.ScissorArray[i].Y,
989                            attr->Scissor.ScissorArray[i].Width,
990                            attr->Scissor.ScissorArray[i].Height);
991          TEST_AND_UPDATE_INDEX(ctx->Scissor.EnableFlags,
992                                attr->Scissor.EnableFlags, i, GL_SCISSOR_TEST);
993       }
994       if (ctx->Extensions.EXT_window_rectangles) {
995          STATIC_ASSERT(sizeof(struct gl_scissor_rect) ==
996                        4 * sizeof(GLint));
997          _mesa_WindowRectanglesEXT(
998                attr->Scissor.WindowRectMode, attr->Scissor.NumWindowRects,
999                (const GLint *)attr->Scissor.WindowRects);
1000       }
1001    }
1002 
1003    if (mask & GL_STENCIL_BUFFER_BIT) {
1004       TEST_AND_UPDATE(ctx->Stencil.Enabled, attr->Stencil.Enabled,
1005                       GL_STENCIL_TEST);
1006       TEST_AND_CALL1(Stencil.Clear, ClearStencil);
1007       if (ctx->Extensions.EXT_stencil_two_side) {
1008          TEST_AND_UPDATE(ctx->Stencil.TestTwoSide, attr->Stencil.TestTwoSide,
1009                          GL_STENCIL_TEST_TWO_SIDE_EXT);
1010          _mesa_ActiveStencilFaceEXT(attr->Stencil.ActiveFace
1011                                     ? GL_BACK : GL_FRONT);
1012       }
1013       /* front state */
1014       _mesa_StencilFuncSeparate(GL_FRONT,
1015                                 attr->Stencil.Function[0],
1016                                 attr->Stencil.Ref[0],
1017                                 attr->Stencil.ValueMask[0]);
1018       TEST_AND_CALL1_SEL(Stencil.WriteMask[0], StencilMaskSeparate, GL_FRONT);
1019       _mesa_StencilOpSeparate(GL_FRONT, attr->Stencil.FailFunc[0],
1020                               attr->Stencil.ZFailFunc[0],
1021                               attr->Stencil.ZPassFunc[0]);
1022       /* back state */
1023       _mesa_StencilFuncSeparate(GL_BACK,
1024                                 attr->Stencil.Function[1],
1025                                 attr->Stencil.Ref[1],
1026                                 attr->Stencil.ValueMask[1]);
1027       TEST_AND_CALL1_SEL(Stencil.WriteMask[1], StencilMaskSeparate, GL_BACK);
1028       _mesa_StencilOpSeparate(GL_BACK, attr->Stencil.FailFunc[1],
1029                               attr->Stencil.ZFailFunc[1],
1030                               attr->Stencil.ZPassFunc[1]);
1031    }
1032 
1033    if (mask & GL_TRANSFORM_BIT) {
1034       GLuint i;
1035       TEST_AND_CALL1(Transform.MatrixMode, MatrixMode);
1036       if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
1037          _math_matrix_analyse(ctx->ProjectionMatrixStack.Top);
1038 
1039       ctx->NewState |= _NEW_TRANSFORM;
1040       ctx->NewDriverState |= ST_NEW_CLIP_STATE;
1041 
1042       /* restore clip planes */
1043       for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
1044          const GLfloat *eyePlane = attr->Transform.EyeUserPlane[i];
1045          COPY_4V(ctx->Transform.EyeUserPlane[i], eyePlane);
1046          TEST_AND_UPDATE_BIT(ctx->Transform.ClipPlanesEnabled,
1047                              attr->Transform.ClipPlanesEnabled, i,
1048                              GL_CLIP_PLANE0 + i);
1049       }
1050 
1051       /* normalize/rescale */
1052       TEST_AND_UPDATE(ctx->Transform.Normalize, attr->Transform.Normalize,
1053                       GL_NORMALIZE);
1054       TEST_AND_UPDATE(ctx->Transform.RescaleNormals,
1055                       attr->Transform.RescaleNormals, GL_RESCALE_NORMAL_EXT);
1056 
1057       if (!ctx->Extensions.AMD_depth_clamp_separate) {
1058          TEST_AND_UPDATE(ctx->Transform.DepthClampNear &&
1059                          ctx->Transform.DepthClampFar,
1060                          attr->Transform.DepthClampNear &&
1061                          attr->Transform.DepthClampFar, GL_DEPTH_CLAMP);
1062       } else {
1063          TEST_AND_UPDATE(ctx->Transform.DepthClampNear,
1064                          attr->Transform.DepthClampNear,
1065                          GL_DEPTH_CLAMP_NEAR_AMD);
1066          TEST_AND_UPDATE(ctx->Transform.DepthClampFar,
1067                          attr->Transform.DepthClampFar,
1068                          GL_DEPTH_CLAMP_FAR_AMD);
1069       }
1070 
1071       if (ctx->Extensions.ARB_clip_control) {
1072          TEST_AND_CALL2(Transform.ClipOrigin, Transform.ClipDepthMode,
1073                         ClipControl);
1074       }
1075    }
1076 
1077    if (mask & GL_TEXTURE_BIT) {
1078       pop_texture_group(ctx, &attr->Texture);
1079       ctx->NewState |= _NEW_TEXTURE_OBJECT | _NEW_TEXTURE_STATE |
1080                        _NEW_FF_VERT_PROGRAM | _NEW_FF_FRAG_PROGRAM;
1081    }
1082 
1083    if (mask & GL_VIEWPORT_BIT) {
1084       unsigned i;
1085 
1086       for (i = 0; i < ctx->Const.MaxViewports; i++) {
1087          const struct gl_viewport_attrib *vp = &attr->Viewport.ViewportArray[i];
1088 
1089          if (memcmp(&ctx->ViewportArray[i].X, &vp->X, sizeof(float) * 6)) {
1090             ctx->NewState |= _NEW_VIEWPORT;
1091             ctx->NewDriverState |= ST_NEW_VIEWPORT;
1092 
1093             memcpy(&ctx->ViewportArray[i].X, &vp->X, sizeof(float) * 6);
1094 
1095             if (ctx->invalidate_on_gl_viewport)
1096                st_manager_invalidate_drawables(ctx);
1097          }
1098       }
1099 
1100       if (ctx->Extensions.NV_conservative_raster) {
1101          GLuint biasx = attr->Viewport.SubpixelPrecisionBias[0];
1102          GLuint biasy = attr->Viewport.SubpixelPrecisionBias[1];
1103          _mesa_SubpixelPrecisionBiasNV(biasx, biasy);
1104       }
1105    }
1106 
1107    if (mask & GL_MULTISAMPLE_BIT_ARB) {
1108       TEST_AND_UPDATE(ctx->Multisample.Enabled,
1109                       attr->Multisample.Enabled,
1110                       GL_MULTISAMPLE);
1111 
1112       TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
1113                       attr->Multisample.SampleCoverage,
1114                       GL_SAMPLE_COVERAGE);
1115 
1116       TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
1117                       attr->Multisample.SampleAlphaToCoverage,
1118                       GL_SAMPLE_ALPHA_TO_COVERAGE);
1119 
1120       TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
1121                       attr->Multisample.SampleAlphaToOne,
1122                       GL_SAMPLE_ALPHA_TO_ONE);
1123 
1124       TEST_AND_CALL2(Multisample.SampleCoverageValue,
1125                      Multisample.SampleCoverageInvert, SampleCoverage);
1126 
1127       TEST_AND_CALL1(Multisample.SampleAlphaToCoverageDitherControl,
1128                      AlphaToCoverageDitherControlNV);
1129    }
1130 
1131    ctx->PopAttribState = attr->OldPopAttribStateMask;
1132 }
1133 
1134 
1135 /**
1136  * Copy gl_pixelstore_attrib from src to dst, updating buffer
1137  * object refcounts.
1138  */
1139 static void
copy_pixelstore(struct gl_context * ctx,struct gl_pixelstore_attrib * dst,const struct gl_pixelstore_attrib * src)1140 copy_pixelstore(struct gl_context *ctx,
1141                 struct gl_pixelstore_attrib *dst,
1142                 const struct gl_pixelstore_attrib *src)
1143 {
1144    dst->Alignment = src->Alignment;
1145    dst->RowLength = src->RowLength;
1146    dst->SkipPixels = src->SkipPixels;
1147    dst->SkipRows = src->SkipRows;
1148    dst->ImageHeight = src->ImageHeight;
1149    dst->SkipImages = src->SkipImages;
1150    dst->SwapBytes = src->SwapBytes;
1151    dst->LsbFirst = src->LsbFirst;
1152    dst->Invert = src->Invert;
1153    _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
1154 }
1155 
1156 
1157 #define GL_CLIENT_PACK_BIT (1<<20)
1158 #define GL_CLIENT_UNPACK_BIT (1<<21)
1159 
1160 static void
copy_vertex_attrib_array(struct gl_context * ctx,struct gl_array_attributes * dst,const struct gl_array_attributes * src)1161 copy_vertex_attrib_array(struct gl_context *ctx,
1162                          struct gl_array_attributes *dst,
1163                          const struct gl_array_attributes *src)
1164 {
1165    dst->Ptr            = src->Ptr;
1166    dst->RelativeOffset = src->RelativeOffset;
1167    dst->Format         = src->Format;
1168    dst->Stride         = src->Stride;
1169    dst->BufferBindingIndex = src->BufferBindingIndex;
1170    dst->_EffBufferBindingIndex = src->_EffBufferBindingIndex;
1171    dst->_EffRelativeOffset = src->_EffRelativeOffset;
1172 }
1173 
1174 static void
copy_vertex_buffer_binding(struct gl_context * ctx,struct gl_vertex_buffer_binding * dst,const struct gl_vertex_buffer_binding * src)1175 copy_vertex_buffer_binding(struct gl_context *ctx,
1176                            struct gl_vertex_buffer_binding *dst,
1177                            const struct gl_vertex_buffer_binding *src)
1178 {
1179    dst->Offset          = src->Offset;
1180    dst->Stride          = src->Stride;
1181    dst->InstanceDivisor = src->InstanceDivisor;
1182    dst->_BoundArrays    = src->_BoundArrays;
1183    dst->_EffBoundArrays = src->_EffBoundArrays;
1184    dst->_EffOffset      = src->_EffOffset;
1185 
1186    _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
1187 }
1188 
1189 /**
1190  * Copy gl_vertex_array_object from src to dest.
1191  * 'dest' must be in an initialized state.
1192  */
1193 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)1194 copy_array_object(struct gl_context *ctx,
1195                   struct gl_vertex_array_object *dest,
1196                   struct gl_vertex_array_object *src,
1197                   unsigned copy_attrib_mask)
1198 {
1199    /* skip Name */
1200    /* skip RefCount */
1201 
1202    while (copy_attrib_mask) {
1203       unsigned i = u_bit_scan(&copy_attrib_mask);
1204 
1205       copy_vertex_attrib_array(ctx, &dest->VertexAttrib[i], &src->VertexAttrib[i]);
1206       copy_vertex_buffer_binding(ctx, &dest->BufferBinding[i], &src->BufferBinding[i]);
1207    }
1208 
1209    /* Enabled must be the same than on push */
1210    dest->Enabled = src->Enabled;
1211    dest->_EnabledWithMapMode = src->_EnabledWithMapMode;
1212    dest->_EffEnabledVBO = src->_EffEnabledVBO;
1213    dest->_EffEnabledNonZeroDivisor = src->_EffEnabledNonZeroDivisor;
1214    /* The bitmask of bound VBOs needs to match the VertexBinding array */
1215    dest->VertexAttribBufferMask = src->VertexAttribBufferMask;
1216    dest->NonZeroDivisorMask = src->NonZeroDivisorMask;
1217    dest->_AttributeMapMode = src->_AttributeMapMode;
1218    dest->NewVertexBuffers = src->NewVertexBuffers;
1219    dest->NewVertexElements = src->NewVertexElements;
1220    /* skip NumUpdates and IsDynamic because they can only increase, not decrease */
1221 }
1222 
1223 /**
1224  * Copy gl_array_attrib from src to dest.
1225  * 'dest' must be in an initialized state.
1226  */
1227 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)1228 copy_array_attrib(struct gl_context *ctx,
1229                   struct gl_array_attrib *dest,
1230                   struct gl_array_attrib *src,
1231                   bool vbo_deleted,
1232                   unsigned copy_attrib_mask)
1233 {
1234    /* skip ArrayObj */
1235    /* skip DefaultArrayObj, Objects */
1236    dest->ActiveTexture = src->ActiveTexture;
1237    dest->LockFirst = src->LockFirst;
1238    dest->LockCount = src->LockCount;
1239    dest->PrimitiveRestart = src->PrimitiveRestart;
1240    dest->PrimitiveRestartFixedIndex = src->PrimitiveRestartFixedIndex;
1241    dest->RestartIndex = src->RestartIndex;
1242    memcpy(dest->_PrimitiveRestart, src->_PrimitiveRestart,
1243           sizeof(src->_PrimitiveRestart));
1244    memcpy(dest->_RestartIndex, src->_RestartIndex, sizeof(src->_RestartIndex));
1245    /* skip NewState */
1246    /* skip RebindArrays */
1247 
1248    if (!vbo_deleted)
1249       copy_array_object(ctx, dest->VAO, src->VAO, copy_attrib_mask);
1250 
1251    /* skip ArrayBufferObj */
1252    /* skip IndexBufferObj */
1253 }
1254 
1255 /**
1256  * Save the content of src to dest.
1257  */
1258 static void
save_array_attrib(struct gl_context * ctx,struct gl_array_attrib * dest,struct gl_array_attrib * src)1259 save_array_attrib(struct gl_context *ctx,
1260                   struct gl_array_attrib *dest,
1261                   struct gl_array_attrib *src)
1262 {
1263    /* Set the Name, needed for restore, but do never overwrite.
1264     * Needs to match value in the object hash. */
1265    dest->VAO->Name = src->VAO->Name;
1266    dest->VAO->NonDefaultStateMask = src->VAO->NonDefaultStateMask;
1267    /* And copy all of the rest. */
1268    copy_array_attrib(ctx, dest, src, false, src->VAO->NonDefaultStateMask);
1269 
1270    /* Just reference them here */
1271    _mesa_reference_buffer_object(ctx, &dest->ArrayBufferObj,
1272                                  src->ArrayBufferObj);
1273    _mesa_reference_buffer_object(ctx, &dest->VAO->IndexBufferObj,
1274                                  src->VAO->IndexBufferObj);
1275 }
1276 
1277 /**
1278  * Restore the content of src to dest.
1279  */
1280 static void
restore_array_attrib(struct gl_context * ctx,struct gl_array_attrib * dest,struct gl_array_attrib * src)1281 restore_array_attrib(struct gl_context *ctx,
1282                      struct gl_array_attrib *dest,
1283                      struct gl_array_attrib *src)
1284 {
1285    bool is_vao_name_zero = src->VAO->Name == 0;
1286 
1287    /* The ARB_vertex_array_object spec says:
1288     *
1289     *     "BindVertexArray fails and an INVALID_OPERATION error is generated
1290     *     if array is not a name returned from a previous call to
1291     *     GenVertexArrays, or if such a name has since been deleted with
1292     *     DeleteVertexArrays."
1293     *
1294     * Therefore popping a deleted VAO cannot magically recreate it.
1295     */
1296    if (!is_vao_name_zero && !_mesa_IsVertexArray(src->VAO->Name))
1297       return;
1298 
1299    _mesa_BindVertexArray(src->VAO->Name);
1300 
1301    /* Restore or recreate the buffer objects by the names ... */
1302    if (is_vao_name_zero || !src->ArrayBufferObj ||
1303        _mesa_IsBuffer(src->ArrayBufferObj->Name)) {
1304       /* ... and restore its content */
1305       dest->VAO->NonDefaultStateMask |= src->VAO->NonDefaultStateMask;
1306       copy_array_attrib(ctx, dest, src, false,
1307                         dest->VAO->NonDefaultStateMask);
1308 
1309       _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB,
1310                        src->ArrayBufferObj ?
1311                           src->ArrayBufferObj->Name : 0);
1312    } else {
1313       copy_array_attrib(ctx, dest, src, true, 0);
1314    }
1315 
1316    /* Invalidate array state. It will be updated during the next draw. */
1317    _mesa_set_draw_vao(ctx, ctx->Array._EmptyVAO, 0);
1318 
1319    if (is_vao_name_zero || !src->VAO->IndexBufferObj ||
1320        _mesa_IsBuffer(src->VAO->IndexBufferObj->Name)) {
1321       _mesa_BindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,
1322                        src->VAO->IndexBufferObj ?
1323                           src->VAO->IndexBufferObj->Name : 0);
1324    }
1325 }
1326 
1327 
1328 void GLAPIENTRY
_mesa_PushClientAttrib(GLbitfield mask)1329 _mesa_PushClientAttrib(GLbitfield mask)
1330 {
1331    struct gl_client_attrib_node *head;
1332 
1333    GET_CURRENT_CONTEXT(ctx);
1334 
1335    if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
1336       _mesa_error(ctx, GL_STACK_OVERFLOW, "glPushClientAttrib");
1337       return;
1338    }
1339 
1340    head = &ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
1341    head->Mask = mask;
1342 
1343    if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
1344       copy_pixelstore(ctx, &head->Pack, &ctx->Pack);
1345       copy_pixelstore(ctx, &head->Unpack, &ctx->Unpack);
1346    }
1347 
1348    if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1349       _mesa_initialize_vao(ctx, &head->VAO, 0);
1350       /* Use the VAO declared within the node instead of allocating it. */
1351       head->Array.VAO = &head->VAO;
1352       save_array_attrib(ctx, &head->Array, &ctx->Array);
1353    }
1354 
1355    ctx->ClientAttribStackDepth++;
1356 }
1357 
1358 
1359 void GLAPIENTRY
_mesa_PopClientAttrib(void)1360 _mesa_PopClientAttrib(void)
1361 {
1362    struct gl_client_attrib_node *head;
1363 
1364    GET_CURRENT_CONTEXT(ctx);
1365 
1366    if (ctx->ClientAttribStackDepth == 0) {
1367       _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib");
1368       return;
1369    }
1370 
1371    ctx->ClientAttribStackDepth--;
1372    head = &ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
1373 
1374    if (head->Mask & GL_CLIENT_PIXEL_STORE_BIT) {
1375       copy_pixelstore(ctx, &ctx->Pack, &head->Pack);
1376       _mesa_reference_buffer_object(ctx, &head->Pack.BufferObj, NULL);
1377 
1378       copy_pixelstore(ctx, &ctx->Unpack, &head->Unpack);
1379       _mesa_reference_buffer_object(ctx, &head->Unpack.BufferObj, NULL);
1380    }
1381 
1382    if (head->Mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1383       restore_array_attrib(ctx, &ctx->Array, &head->Array);
1384 
1385       /* _mesa_unbind_array_object_vbos can't use NonDefaultStateMask because
1386        * it's used by internal VAOs which don't always update the mask, so do
1387        * it manually here.
1388        */
1389       GLbitfield mask = head->VAO.NonDefaultStateMask;
1390       while (mask) {
1391          unsigned i = u_bit_scan(&mask);
1392          _mesa_reference_buffer_object(ctx, &head->VAO.BufferBinding[i].BufferObj, NULL);
1393       }
1394 
1395       _mesa_reference_buffer_object(ctx, &head->VAO.IndexBufferObj, NULL);
1396       _mesa_reference_buffer_object(ctx, &head->Array.ArrayBufferObj, NULL);
1397    }
1398 }
1399 
1400 void GLAPIENTRY
_mesa_ClientAttribDefaultEXT(GLbitfield mask)1401 _mesa_ClientAttribDefaultEXT( GLbitfield mask )
1402 {
1403    if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
1404       _mesa_PixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
1405       _mesa_PixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
1406       _mesa_PixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
1407       _mesa_PixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
1408       _mesa_PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1409       _mesa_PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
1410       _mesa_PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
1411       _mesa_PixelStorei(GL_UNPACK_ALIGNMENT, 4);
1412       _mesa_PixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
1413       _mesa_PixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
1414       _mesa_PixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
1415       _mesa_PixelStorei(GL_PACK_SKIP_IMAGES, 0);
1416       _mesa_PixelStorei(GL_PACK_ROW_LENGTH, 0);
1417       _mesa_PixelStorei(GL_PACK_SKIP_ROWS, 0);
1418       _mesa_PixelStorei(GL_PACK_SKIP_PIXELS, 0);
1419       _mesa_PixelStorei(GL_PACK_ALIGNMENT, 4);
1420 
1421       _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1422       _mesa_BindBuffer(GL_PIXEL_PACK_BUFFER, 0);
1423    }
1424    if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1425       GET_CURRENT_CONTEXT(ctx);
1426       int i;
1427 
1428       _mesa_BindBuffer(GL_ARRAY_BUFFER, 0);
1429       _mesa_BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1430 
1431       _mesa_DisableClientState(GL_EDGE_FLAG_ARRAY);
1432       _mesa_EdgeFlagPointer(0, 0);
1433 
1434       _mesa_DisableClientState(GL_INDEX_ARRAY);
1435       _mesa_IndexPointer(GL_FLOAT, 0, 0);
1436 
1437       _mesa_DisableClientState(GL_SECONDARY_COLOR_ARRAY);
1438       _mesa_SecondaryColorPointer(4, GL_FLOAT, 0, 0);
1439 
1440       _mesa_DisableClientState(GL_FOG_COORD_ARRAY);
1441       _mesa_FogCoordPointer(GL_FLOAT, 0, 0);
1442 
1443       for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
1444          _mesa_ClientActiveTexture(GL_TEXTURE0 + i);
1445          _mesa_DisableClientState(GL_TEXTURE_COORD_ARRAY);
1446          _mesa_TexCoordPointer(4, GL_FLOAT, 0, 0);
1447       }
1448 
1449       _mesa_DisableClientState(GL_COLOR_ARRAY);
1450       _mesa_ColorPointer(4, GL_FLOAT, 0, 0);
1451 
1452       _mesa_DisableClientState(GL_NORMAL_ARRAY);
1453       _mesa_NormalPointer(GL_FLOAT, 0, 0);
1454 
1455       _mesa_DisableClientState(GL_VERTEX_ARRAY);
1456       _mesa_VertexPointer(4, GL_FLOAT, 0, 0);
1457 
1458       for (i = 0; i < ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs; i++) {
1459          _mesa_DisableVertexAttribArray(i);
1460          _mesa_VertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, 0, 0);
1461       }
1462 
1463       _mesa_ClientActiveTexture(GL_TEXTURE0);
1464 
1465       _mesa_PrimitiveRestartIndex_no_error(0);
1466       if (ctx->Version >= 31)
1467          _mesa_Disable(GL_PRIMITIVE_RESTART);
1468       else if (_mesa_has_NV_primitive_restart(ctx))
1469          _mesa_DisableClientState(GL_PRIMITIVE_RESTART_NV);
1470 
1471       if (_mesa_has_ARB_ES3_compatibility(ctx))
1472          _mesa_Disable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
1473    }
1474 }
1475 
1476 void GLAPIENTRY
_mesa_PushClientAttribDefaultEXT(GLbitfield mask)1477 _mesa_PushClientAttribDefaultEXT( GLbitfield mask )
1478 {
1479    _mesa_PushClientAttrib(mask);
1480    _mesa_ClientAttribDefaultEXT(mask);
1481 }
1482 
1483 
1484 /**
1485  * Free any attribute state data that might be attached to the context.
1486  */
1487 void
_mesa_free_attrib_data(struct gl_context * ctx)1488 _mesa_free_attrib_data(struct gl_context *ctx)
1489 {
1490    for (unsigned i = 0; i < ARRAY_SIZE(ctx->AttribStack); i++)
1491       FREE(ctx->AttribStack[i]);
1492 }
1493 
1494 
1495 void
_mesa_init_attrib(struct gl_context * ctx)1496 _mesa_init_attrib(struct gl_context *ctx)
1497 {
1498    /* Renderer and client attribute stacks */
1499    ctx->AttribStackDepth = 0;
1500    ctx->ClientAttribStackDepth = 0;
1501 }
1502