xref: /aosp_15_r20/external/mesa3d/src/mesa/main/attrib.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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. Also reset the attributes stored in vbo, as after this we'll
686     * change Current directly, and these changed values would've been then
687     * overridden by another flush in the future.
688     */
689    if ((mask & GL_CURRENT_BIT) && ctx->Driver.NeedFlush) {
690       FLUSH_CURRENT(ctx, 0);
691       vbo_reset_all_attr(ctx);
692    }
693 
694    /* Only restore states that have been changed since glPushAttrib. */
695    mask &= ctx->PopAttribState;
696 
697    if (mask & GL_ACCUM_BUFFER_BIT) {
698       _mesa_ClearAccum(attr->Accum.ClearColor[0],
699                        attr->Accum.ClearColor[1],
700                        attr->Accum.ClearColor[2],
701                        attr->Accum.ClearColor[3]);
702    }
703 
704    if (mask & GL_COLOR_BUFFER_BIT) {
705       TEST_AND_CALL1(Color.ClearIndex, ClearIndex);
706       _mesa_ClearColor(attr->Color.ClearColor.f[0],
707                        attr->Color.ClearColor.f[1],
708                        attr->Color.ClearColor.f[2],
709                        attr->Color.ClearColor.f[3]);
710       TEST_AND_CALL1(Color.IndexMask, IndexMask);
711       if (ctx->Color.ColorMask != attr->Color.ColorMask) {
712          if (!ctx->Extensions.EXT_draw_buffers2) {
713             _mesa_ColorMask(GET_COLORMASK_BIT(attr->Color.ColorMask, 0, 0),
714                             GET_COLORMASK_BIT(attr->Color.ColorMask, 0, 1),
715                             GET_COLORMASK_BIT(attr->Color.ColorMask, 0, 2),
716                             GET_COLORMASK_BIT(attr->Color.ColorMask, 0, 3));
717          } else {
718             for (unsigned i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
719                _mesa_ColorMaski(i,
720                                 GET_COLORMASK_BIT(attr->Color.ColorMask, i, 0),
721                                 GET_COLORMASK_BIT(attr->Color.ColorMask, i, 1),
722                                 GET_COLORMASK_BIT(attr->Color.ColorMask, i, 2),
723                                 GET_COLORMASK_BIT(attr->Color.ColorMask, i, 3));
724             }
725          }
726       }
727       if (memcmp(ctx->Color.DrawBuffer, attr->Color.DrawBuffer,
728                  sizeof(attr->Color.DrawBuffer))) {
729          /* Need to determine if more than one color output is
730           * specified.  If so, call glDrawBuffersARB, else call
731           * glDrawBuffer().  This is a subtle, but essential point
732           * since GL_FRONT (for example) is illegal for the former
733           * function, but legal for the later.
734           */
735          GLboolean multipleBuffers = GL_FALSE;
736          GLuint i;
737 
738          for (i = 1; i < ctx->Const.MaxDrawBuffers; i++) {
739             if (attr->Color.DrawBuffer[i] != GL_NONE) {
740                multipleBuffers = GL_TRUE;
741                break;
742             }
743          }
744          /* Call the API_level functions, not _mesa_drawbuffers()
745           * since we need to do error checking on the pop'd
746           * GL_DRAW_BUFFER.
747           * Ex: if GL_FRONT were pushed, but we're popping with a
748           * user FBO bound, GL_FRONT will be illegal and we'll need
749           * to record that error.  Per OpenGL ARB decision.
750           */
751          if (multipleBuffers) {
752             GLenum buffers[MAX_DRAW_BUFFERS];
753 
754             for (unsigned i = 0; i < ctx->Const.MaxDrawBuffers; i++)
755                buffers[i] = attr->Color.DrawBuffer[i];
756 
757             _mesa_DrawBuffers(ctx->Const.MaxDrawBuffers, buffers);
758          } else {
759             _mesa_DrawBuffer(attr->Color.DrawBuffer[0]);
760          }
761       }
762       TEST_AND_UPDATE(ctx->Color.AlphaEnabled, attr->Color.AlphaEnabled,
763                       GL_ALPHA_TEST);
764       TEST_AND_CALL2(Color.AlphaFunc, Color.AlphaRefUnclamped, AlphaFunc);
765       if (ctx->Color.BlendEnabled != attr->Color.BlendEnabled) {
766          if (ctx->Extensions.EXT_draw_buffers2) {
767             for (unsigned i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
768                TEST_AND_UPDATE_INDEX(ctx->Color.BlendEnabled,
769                                      attr->Color.BlendEnabled, i, GL_BLEND);
770             }
771          }
772          else {
773             TEST_AND_UPDATE(ctx->Color.BlendEnabled & 0x1,
774                             attr->Color.BlendEnabled & 0x1, GL_BLEND);
775          }
776       }
777       if (ctx->Color._BlendFuncPerBuffer ||
778           ctx->Color._BlendEquationPerBuffer) {
779          /* set blend per buffer */
780          GLuint buf;
781          for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) {
782             _mesa_BlendFuncSeparateiARB(buf, attr->Color.Blend[buf].SrcRGB,
783                                      attr->Color.Blend[buf].DstRGB,
784                                      attr->Color.Blend[buf].SrcA,
785                                      attr->Color.Blend[buf].DstA);
786             _mesa_BlendEquationSeparateiARB(buf,
787                                          attr->Color.Blend[buf].EquationRGB,
788                                          attr->Color.Blend[buf].EquationA);
789          }
790       }
791       else {
792          /* set same blend modes for all buffers */
793          _mesa_BlendFuncSeparate(attr->Color.Blend[0].SrcRGB,
794                                     attr->Color.Blend[0].DstRGB,
795                                     attr->Color.Blend[0].SrcA,
796                                     attr->Color.Blend[0].DstA);
797          /* This special case is because glBlendEquationSeparateEXT
798           * cannot take GL_LOGIC_OP as a parameter.
799           */
800          if (attr->Color.Blend[0].EquationRGB ==
801              attr->Color.Blend[0].EquationA) {
802             TEST_AND_CALL1(Color.Blend[0].EquationRGB, BlendEquation);
803          }
804          else {
805             TEST_AND_CALL2(Color.Blend[0].EquationRGB,
806                            Color.Blend[0].EquationA, BlendEquationSeparate);
807          }
808       }
809       _mesa_BlendColor(attr->Color.BlendColorUnclamped[0],
810                        attr->Color.BlendColorUnclamped[1],
811                        attr->Color.BlendColorUnclamped[2],
812                        attr->Color.BlendColorUnclamped[3]);
813       TEST_AND_CALL1(Color.LogicOp, LogicOp);
814       TEST_AND_UPDATE(ctx->Color.ColorLogicOpEnabled,
815                       attr->Color.ColorLogicOpEnabled, GL_COLOR_LOGIC_OP);
816       TEST_AND_UPDATE(ctx->Color.IndexLogicOpEnabled,
817                       attr->Color.IndexLogicOpEnabled, GL_INDEX_LOGIC_OP);
818       TEST_AND_UPDATE(ctx->Color.DitherFlag, attr->Color.DitherFlag,
819                       GL_DITHER);
820       if (ctx->Extensions.ARB_color_buffer_float) {
821          TEST_AND_CALL1_SEL(Color.ClampFragmentColor, ClampColor,
822                             GL_CLAMP_FRAGMENT_COLOR);
823       }
824       if (ctx->Extensions.ARB_color_buffer_float || ctx->Version >= 30) {
825          TEST_AND_CALL1_SEL(Color.ClampReadColor, ClampColor,
826                             GL_CLAMP_READ_COLOR);
827       }
828       /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
829       if (ctx->Extensions.EXT_framebuffer_sRGB) {
830          TEST_AND_UPDATE(ctx->Color.sRGBEnabled, attr->Color.sRGBEnabled,
831                          GL_FRAMEBUFFER_SRGB);
832       }
833    }
834 
835    if (mask & GL_CURRENT_BIT) {
836       memcpy(&ctx->Current, &attr->Current,
837              sizeof(struct gl_current_attrib));
838       ctx->NewState |= _NEW_CURRENT_ATTRIB;
839    }
840 
841    if (mask & GL_DEPTH_BUFFER_BIT) {
842       TEST_AND_CALL1(Depth.Func, DepthFunc);
843       TEST_AND_CALL1(Depth.Clear, ClearDepth);
844       TEST_AND_UPDATE(ctx->Depth.Test, attr->Depth.Test, GL_DEPTH_TEST);
845       TEST_AND_CALL1(Depth.Mask, DepthMask);
846       if (ctx->Extensions.EXT_depth_bounds_test) {
847          TEST_AND_UPDATE(ctx->Depth.BoundsTest, attr->Depth.BoundsTest,
848                          GL_DEPTH_BOUNDS_TEST_EXT);
849          TEST_AND_CALL2(Depth.BoundsMin, Depth.BoundsMax, DepthBoundsEXT);
850       }
851    }
852 
853    if (mask & GL_ENABLE_BIT)
854       pop_enable_group(ctx, &attr->Enable);
855 
856    if (mask & GL_EVAL_BIT) {
857       memcpy(&ctx->Eval, &attr->Eval, sizeof(struct gl_eval_attrib));
858       vbo_exec_update_eval_maps(ctx);
859    }
860 
861    if (mask & GL_FOG_BIT) {
862       TEST_AND_UPDATE(ctx->Fog.Enabled, attr->Fog.Enabled, GL_FOG);
863       _mesa_Fogfv(GL_FOG_COLOR, attr->Fog.Color);
864       TEST_AND_CALL1_SEL(Fog.Density, Fogf, GL_FOG_DENSITY);
865       TEST_AND_CALL1_SEL(Fog.Start, Fogf, GL_FOG_START);
866       TEST_AND_CALL1_SEL(Fog.End, Fogf, GL_FOG_END);
867       TEST_AND_CALL1_SEL(Fog.Index, Fogf, GL_FOG_INDEX);
868       TEST_AND_CALL1_SEL(Fog.Mode, Fogi, GL_FOG_MODE);
869    }
870 
871    if (mask & GL_HINT_BIT) {
872       TEST_AND_CALL1_SEL(Hint.PerspectiveCorrection, Hint, GL_PERSPECTIVE_CORRECTION_HINT);
873       TEST_AND_CALL1_SEL(Hint.PointSmooth, Hint, GL_POINT_SMOOTH_HINT);
874       TEST_AND_CALL1_SEL(Hint.LineSmooth, Hint, GL_LINE_SMOOTH_HINT);
875       TEST_AND_CALL1_SEL(Hint.PolygonSmooth, Hint, GL_POLYGON_SMOOTH_HINT);
876       TEST_AND_CALL1_SEL(Hint.Fog, Hint, GL_FOG_HINT);
877       TEST_AND_CALL1_SEL(Hint.TextureCompression, Hint, GL_TEXTURE_COMPRESSION_HINT_ARB);
878    }
879 
880    if (mask & GL_LIGHTING_BIT) {
881       GLuint i;
882       /* lighting enable */
883       TEST_AND_UPDATE(ctx->Light.Enabled, attr->Light.Enabled, GL_LIGHTING);
884       /* per-light state */
885       if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
886          _math_matrix_analyse(ctx->ModelviewMatrixStack.Top);
887 
888       /* Fast path for other drivers. */
889       ctx->NewState |= _NEW_LIGHT_CONSTANTS | _NEW_FF_VERT_PROGRAM;
890 
891       memcpy(ctx->Light.LightSource, attr->Light.LightSource,
892              sizeof(attr->Light.LightSource));
893       memcpy(&ctx->Light.Model, &attr->Light.Model,
894              sizeof(attr->Light.Model));
895 
896       for (i = 0; i < ctx->Const.MaxLights; i++) {
897          TEST_AND_UPDATE(ctx->Light.Light[i].Enabled,
898                          attr->Light.Light[i].Enabled,
899                          GL_LIGHT0 + i);
900          memcpy(&ctx->Light.Light[i], &attr->Light.Light[i],
901                 sizeof(struct gl_light));
902       }
903       /* shade model */
904       TEST_AND_CALL1(Light.ShadeModel, ShadeModel);
905       /* color material */
906       TEST_AND_CALL2(Light.ColorMaterialFace, Light.ColorMaterialMode,
907                      ColorMaterial);
908       TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled,
909                       attr->Light.ColorMaterialEnabled, GL_COLOR_MATERIAL);
910       /* Shininess material is used by the fixed-func vertex program. */
911       ctx->NewState |= _NEW_MATERIAL | _NEW_FF_VERT_PROGRAM;
912       memcpy(&ctx->Light.Material, &attr->Light.Material,
913              sizeof(struct gl_material));
914       if (ctx->Extensions.ARB_color_buffer_float) {
915          TEST_AND_CALL1_SEL(Light.ClampVertexColor, ClampColor, GL_CLAMP_VERTEX_COLOR_ARB);
916       }
917    }
918 
919    if (mask & GL_LINE_BIT) {
920       TEST_AND_UPDATE(ctx->Line.SmoothFlag, attr->Line.SmoothFlag, GL_LINE_SMOOTH);
921       TEST_AND_UPDATE(ctx->Line.StippleFlag, attr->Line.StippleFlag, GL_LINE_STIPPLE);
922       TEST_AND_CALL2(Line.StippleFactor, Line.StipplePattern, LineStipple);
923       TEST_AND_CALL1(Line.Width, LineWidth);
924    }
925 
926    if (mask & GL_LIST_BIT)
927       memcpy(&ctx->List, &attr->List, sizeof(struct gl_list_attrib));
928 
929    if (mask & GL_PIXEL_MODE_BIT) {
930       memcpy(&ctx->Pixel, &attr->Pixel, sizeof(struct gl_pixel_attrib));
931       /* XXX what other pixel state needs to be set by function calls? */
932       _mesa_ReadBuffer(ctx->Pixel.ReadBuffer);
933       ctx->NewState |= _NEW_PIXEL;
934    }
935 
936    if (mask & GL_POINT_BIT) {
937       TEST_AND_CALL1(Point.Size, PointSize);
938       TEST_AND_UPDATE(ctx->Point.SmoothFlag, attr->Point.SmoothFlag, GL_POINT_SMOOTH);
939       _mesa_PointParameterfv(GL_DISTANCE_ATTENUATION_EXT, attr->Point.Params);
940       TEST_AND_CALL1_SEL(Point.MinSize, PointParameterf, GL_POINT_SIZE_MIN_EXT);
941       TEST_AND_CALL1_SEL(Point.MaxSize, PointParameterf, GL_POINT_SIZE_MAX_EXT);
942       TEST_AND_CALL1_SEL(Point.Threshold, PointParameterf, GL_POINT_FADE_THRESHOLD_SIZE_EXT);
943 
944       if (ctx->Point.CoordReplace != attr->Point.CoordReplace) {
945          ctx->NewState |= _NEW_POINT | _NEW_FF_VERT_PROGRAM;
946          ctx->Point.CoordReplace = attr->Point.CoordReplace;
947       }
948       TEST_AND_UPDATE(ctx->Point.PointSprite, attr->Point.PointSprite,
949                       GL_POINT_SPRITE);
950 
951       if ((_mesa_is_desktop_gl_compat(ctx) && ctx->Version >= 20)
952           || _mesa_is_desktop_gl_core(ctx))
953          TEST_AND_CALL1_SEL(Point.SpriteOrigin, PointParameterf, GL_POINT_SPRITE_COORD_ORIGIN);
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    /* Restore the previous PopAttribStateMask as well as any modified state
1132     * that was not restored in the current pop.
1133     */
1134    ctx->PopAttribState = attr->OldPopAttribStateMask |
1135                          (ctx->PopAttribState & ~attr->Mask);
1136 }
1137 
1138 
1139 /**
1140  * Copy gl_pixelstore_attrib from src to dst, updating buffer
1141  * object refcounts.
1142  */
1143 static void
copy_pixelstore(struct gl_context * ctx,struct gl_pixelstore_attrib * dst,const struct gl_pixelstore_attrib * src)1144 copy_pixelstore(struct gl_context *ctx,
1145                 struct gl_pixelstore_attrib *dst,
1146                 const struct gl_pixelstore_attrib *src)
1147 {
1148    dst->Alignment = src->Alignment;
1149    dst->RowLength = src->RowLength;
1150    dst->SkipPixels = src->SkipPixels;
1151    dst->SkipRows = src->SkipRows;
1152    dst->ImageHeight = src->ImageHeight;
1153    dst->SkipImages = src->SkipImages;
1154    dst->SwapBytes = src->SwapBytes;
1155    dst->LsbFirst = src->LsbFirst;
1156    dst->Invert = src->Invert;
1157    _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
1158 }
1159 
1160 
1161 #define GL_CLIENT_PACK_BIT (1<<20)
1162 #define GL_CLIENT_UNPACK_BIT (1<<21)
1163 
1164 static void
copy_vertex_attrib_array(struct gl_context * ctx,struct gl_array_attributes * dst,const struct gl_array_attributes * src)1165 copy_vertex_attrib_array(struct gl_context *ctx,
1166                          struct gl_array_attributes *dst,
1167                          const struct gl_array_attributes *src)
1168 {
1169    dst->Ptr            = src->Ptr;
1170    dst->RelativeOffset = src->RelativeOffset;
1171    dst->Format         = src->Format;
1172    dst->Stride         = src->Stride;
1173    dst->BufferBindingIndex = src->BufferBindingIndex;
1174    dst->_EffBufferBindingIndex = src->_EffBufferBindingIndex;
1175    dst->_EffRelativeOffset = src->_EffRelativeOffset;
1176 }
1177 
1178 static void
copy_vertex_buffer_binding(struct gl_context * ctx,struct gl_vertex_buffer_binding * dst,const struct gl_vertex_buffer_binding * src)1179 copy_vertex_buffer_binding(struct gl_context *ctx,
1180                            struct gl_vertex_buffer_binding *dst,
1181                            const struct gl_vertex_buffer_binding *src)
1182 {
1183    dst->Offset          = src->Offset;
1184    dst->Stride          = src->Stride;
1185    dst->InstanceDivisor = src->InstanceDivisor;
1186    dst->_BoundArrays    = src->_BoundArrays;
1187    dst->_EffBoundArrays = src->_EffBoundArrays;
1188    dst->_EffOffset      = src->_EffOffset;
1189 
1190    _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
1191 }
1192 
1193 /**
1194  * Copy gl_vertex_array_object from src to dest.
1195  * 'dest' must be in an initialized state.
1196  */
1197 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)1198 copy_array_object(struct gl_context *ctx,
1199                   struct gl_vertex_array_object *dest,
1200                   struct gl_vertex_array_object *src,
1201                   unsigned copy_attrib_mask)
1202 {
1203    /* skip Name */
1204    /* skip RefCount */
1205 
1206    while (copy_attrib_mask) {
1207       unsigned i = u_bit_scan(&copy_attrib_mask);
1208 
1209       copy_vertex_attrib_array(ctx, &dest->VertexAttrib[i], &src->VertexAttrib[i]);
1210       copy_vertex_buffer_binding(ctx, &dest->BufferBinding[i], &src->BufferBinding[i]);
1211    }
1212 
1213    /* Enabled must be the same than on push */
1214    dest->Enabled = src->Enabled;
1215    dest->_EnabledWithMapMode = src->_EnabledWithMapMode;
1216    /* The bitmask of bound VBOs needs to match the VertexBinding array */
1217    dest->VertexAttribBufferMask = src->VertexAttribBufferMask;
1218    dest->NonZeroDivisorMask = src->NonZeroDivisorMask;
1219    dest->NonIdentityBufferAttribMapping = src->NonIdentityBufferAttribMapping;
1220    dest->_AttributeMapMode = src->_AttributeMapMode;
1221    /* skip NumUpdates and IsDynamic because they can only increase, not decrease */
1222 }
1223 
1224 /**
1225  * Copy gl_array_attrib from src to dest.
1226  * 'dest' must be in an initialized state.
1227  */
1228 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)1229 copy_array_attrib(struct gl_context *ctx,
1230                   struct gl_array_attrib *dest,
1231                   struct gl_array_attrib *src,
1232                   bool vbo_deleted,
1233                   unsigned copy_attrib_mask)
1234 {
1235    /* skip ArrayObj */
1236    /* skip DefaultArrayObj, Objects */
1237    dest->ActiveTexture = src->ActiveTexture;
1238    dest->LockFirst = src->LockFirst;
1239    dest->LockCount = src->LockCount;
1240    dest->PrimitiveRestart = src->PrimitiveRestart;
1241    dest->PrimitiveRestartFixedIndex = src->PrimitiveRestartFixedIndex;
1242    dest->RestartIndex = src->RestartIndex;
1243    memcpy(dest->_PrimitiveRestart, src->_PrimitiveRestart,
1244           sizeof(src->_PrimitiveRestart));
1245    memcpy(dest->_RestartIndex, src->_RestartIndex, sizeof(src->_RestartIndex));
1246    /* skip NewState */
1247    /* skip RebindArrays */
1248 
1249    if (!vbo_deleted)
1250       copy_array_object(ctx, dest->VAO, src->VAO, copy_attrib_mask);
1251 
1252    /* skip ArrayBufferObj */
1253    /* skip IndexBufferObj */
1254 }
1255 
1256 /**
1257  * Save the content of src to dest.
1258  */
1259 static void
save_array_attrib(struct gl_context * ctx,struct gl_array_attrib * dest,struct gl_array_attrib * src)1260 save_array_attrib(struct gl_context *ctx,
1261                   struct gl_array_attrib *dest,
1262                   struct gl_array_attrib *src)
1263 {
1264    /* Set the Name, needed for restore, but do never overwrite.
1265     * Needs to match value in the object hash. */
1266    dest->VAO->Name = src->VAO->Name;
1267    dest->VAO->NonDefaultStateMask = src->VAO->NonDefaultStateMask;
1268    /* And copy all of the rest. */
1269    copy_array_attrib(ctx, dest, src, false, src->VAO->NonDefaultStateMask);
1270 
1271    /* Just reference them here */
1272    _mesa_reference_buffer_object(ctx, &dest->ArrayBufferObj,
1273                                  src->ArrayBufferObj);
1274    _mesa_reference_buffer_object(ctx, &dest->VAO->IndexBufferObj,
1275                                  src->VAO->IndexBufferObj);
1276 }
1277 
1278 /**
1279  * Restore the content of src to dest.
1280  */
1281 static void
restore_array_attrib(struct gl_context * ctx,struct gl_array_attrib * dest,struct gl_array_attrib * src)1282 restore_array_attrib(struct gl_context *ctx,
1283                      struct gl_array_attrib *dest,
1284                      struct gl_array_attrib *src)
1285 {
1286    bool is_vao_name_zero = src->VAO->Name == 0;
1287 
1288    /* The ARB_vertex_array_object spec says:
1289     *
1290     *     "BindVertexArray fails and an INVALID_OPERATION error is generated
1291     *     if array is not a name returned from a previous call to
1292     *     GenVertexArrays, or if such a name has since been deleted with
1293     *     DeleteVertexArrays."
1294     *
1295     * Therefore popping a deleted VAO cannot magically recreate it.
1296     */
1297    if (!is_vao_name_zero && !_mesa_IsVertexArray(src->VAO->Name))
1298       return;
1299 
1300    _mesa_BindVertexArray(src->VAO->Name);
1301 
1302    /* Restore or recreate the buffer objects by the names ... */
1303    if (is_vao_name_zero || !src->ArrayBufferObj ||
1304        _mesa_IsBuffer(src->ArrayBufferObj->Name)) {
1305       /* ... and restore its content */
1306       dest->VAO->NonDefaultStateMask |= src->VAO->NonDefaultStateMask;
1307       copy_array_attrib(ctx, dest, src, false,
1308                         dest->VAO->NonDefaultStateMask);
1309 
1310       _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB,
1311                        src->ArrayBufferObj ?
1312                           src->ArrayBufferObj->Name : 0);
1313    } else {
1314       copy_array_attrib(ctx, dest, src, true, 0);
1315    }
1316 
1317    if (is_vao_name_zero || !src->VAO->IndexBufferObj ||
1318        _mesa_IsBuffer(src->VAO->IndexBufferObj->Name)) {
1319       _mesa_BindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,
1320                        src->VAO->IndexBufferObj ?
1321                           src->VAO->IndexBufferObj->Name : 0);
1322    }
1323 
1324    _mesa_update_edgeflag_state_vao(ctx);
1325    _mesa_set_varying_vp_inputs(ctx, ctx->VertexProgram._VPModeInputFilter &
1326                                ctx->Array.VAO->_EnabledWithMapMode);
1327 }
1328 
1329 
1330 void GLAPIENTRY
_mesa_PushClientAttrib(GLbitfield mask)1331 _mesa_PushClientAttrib(GLbitfield mask)
1332 {
1333    struct gl_client_attrib_node *head;
1334 
1335    GET_CURRENT_CONTEXT(ctx);
1336 
1337    if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
1338       _mesa_error(ctx, GL_STACK_OVERFLOW, "glPushClientAttrib");
1339       return;
1340    }
1341 
1342    head = &ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
1343    head->Mask = mask;
1344 
1345    if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
1346       copy_pixelstore(ctx, &head->Pack, &ctx->Pack);
1347       copy_pixelstore(ctx, &head->Unpack, &ctx->Unpack);
1348    }
1349 
1350    if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1351       _mesa_initialize_vao(ctx, &head->VAO, 0);
1352       /* Use the VAO declared within the node instead of allocating it. */
1353       head->Array.VAO = &head->VAO;
1354       save_array_attrib(ctx, &head->Array, &ctx->Array);
1355    }
1356 
1357    ctx->ClientAttribStackDepth++;
1358 }
1359 
1360 
1361 void GLAPIENTRY
_mesa_PopClientAttrib(void)1362 _mesa_PopClientAttrib(void)
1363 {
1364    struct gl_client_attrib_node *head;
1365 
1366    GET_CURRENT_CONTEXT(ctx);
1367 
1368    if (ctx->ClientAttribStackDepth == 0) {
1369       _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib");
1370       return;
1371    }
1372 
1373    ctx->ClientAttribStackDepth--;
1374    head = &ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
1375 
1376    if (head->Mask & GL_CLIENT_PIXEL_STORE_BIT) {
1377       copy_pixelstore(ctx, &ctx->Pack, &head->Pack);
1378       _mesa_reference_buffer_object(ctx, &head->Pack.BufferObj, NULL);
1379 
1380       copy_pixelstore(ctx, &ctx->Unpack, &head->Unpack);
1381       _mesa_reference_buffer_object(ctx, &head->Unpack.BufferObj, NULL);
1382    }
1383 
1384    if (head->Mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1385       restore_array_attrib(ctx, &ctx->Array, &head->Array);
1386 
1387       /* _mesa_unbind_array_object_vbos can't use NonDefaultStateMask because
1388        * it's used by internal VAOs which don't always update the mask, so do
1389        * it manually here.
1390        */
1391       GLbitfield mask = head->VAO.NonDefaultStateMask;
1392       while (mask) {
1393          unsigned i = u_bit_scan(&mask);
1394          _mesa_reference_buffer_object(ctx, &head->VAO.BufferBinding[i].BufferObj, NULL);
1395       }
1396 
1397       _mesa_reference_buffer_object(ctx, &head->VAO.IndexBufferObj, NULL);
1398       _mesa_reference_buffer_object(ctx, &head->Array.ArrayBufferObj, NULL);
1399    }
1400 }
1401 
1402 void GLAPIENTRY
_mesa_ClientAttribDefaultEXT(GLbitfield mask)1403 _mesa_ClientAttribDefaultEXT( GLbitfield mask )
1404 {
1405    if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
1406       _mesa_PixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
1407       _mesa_PixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
1408       _mesa_PixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
1409       _mesa_PixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
1410       _mesa_PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1411       _mesa_PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
1412       _mesa_PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
1413       _mesa_PixelStorei(GL_UNPACK_ALIGNMENT, 4);
1414       _mesa_PixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
1415       _mesa_PixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
1416       _mesa_PixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
1417       _mesa_PixelStorei(GL_PACK_SKIP_IMAGES, 0);
1418       _mesa_PixelStorei(GL_PACK_ROW_LENGTH, 0);
1419       _mesa_PixelStorei(GL_PACK_SKIP_ROWS, 0);
1420       _mesa_PixelStorei(GL_PACK_SKIP_PIXELS, 0);
1421       _mesa_PixelStorei(GL_PACK_ALIGNMENT, 4);
1422 
1423       _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1424       _mesa_BindBuffer(GL_PIXEL_PACK_BUFFER, 0);
1425    }
1426    if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1427       GET_CURRENT_CONTEXT(ctx);
1428       int i;
1429 
1430       _mesa_BindBuffer(GL_ARRAY_BUFFER, 0);
1431       _mesa_BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1432 
1433       _mesa_DisableClientState(GL_EDGE_FLAG_ARRAY);
1434       _mesa_EdgeFlagPointer(0, 0);
1435 
1436       _mesa_DisableClientState(GL_INDEX_ARRAY);
1437       _mesa_IndexPointer(GL_FLOAT, 0, 0);
1438 
1439       _mesa_DisableClientState(GL_SECONDARY_COLOR_ARRAY);
1440       _mesa_SecondaryColorPointer(4, GL_FLOAT, 0, 0);
1441 
1442       _mesa_DisableClientState(GL_FOG_COORD_ARRAY);
1443       _mesa_FogCoordPointer(GL_FLOAT, 0, 0);
1444 
1445       for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
1446          _mesa_ClientActiveTexture(GL_TEXTURE0 + i);
1447          _mesa_DisableClientState(GL_TEXTURE_COORD_ARRAY);
1448          _mesa_TexCoordPointer(4, GL_FLOAT, 0, 0);
1449       }
1450 
1451       _mesa_DisableClientState(GL_COLOR_ARRAY);
1452       _mesa_ColorPointer(4, GL_FLOAT, 0, 0);
1453 
1454       _mesa_DisableClientState(GL_NORMAL_ARRAY);
1455       _mesa_NormalPointer(GL_FLOAT, 0, 0);
1456 
1457       _mesa_DisableClientState(GL_VERTEX_ARRAY);
1458       _mesa_VertexPointer(4, GL_FLOAT, 0, 0);
1459 
1460       for (i = 0; i < ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs; i++) {
1461          _mesa_DisableVertexAttribArray(i);
1462          _mesa_VertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, 0, 0);
1463       }
1464 
1465       _mesa_ClientActiveTexture(GL_TEXTURE0);
1466 
1467       _mesa_PrimitiveRestartIndex_no_error(0);
1468       if (ctx->Version >= 31)
1469          _mesa_Disable(GL_PRIMITIVE_RESTART);
1470       else if (_mesa_has_NV_primitive_restart(ctx))
1471          _mesa_DisableClientState(GL_PRIMITIVE_RESTART_NV);
1472 
1473       if (_mesa_has_ARB_ES3_compatibility(ctx))
1474          _mesa_Disable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
1475    }
1476 }
1477 
1478 void GLAPIENTRY
_mesa_PushClientAttribDefaultEXT(GLbitfield mask)1479 _mesa_PushClientAttribDefaultEXT( GLbitfield mask )
1480 {
1481    _mesa_PushClientAttrib(mask);
1482    _mesa_ClientAttribDefaultEXT(mask);
1483 }
1484 
1485 
1486 /**
1487  * Free any attribute state data that might be attached to the context.
1488  */
1489 void
_mesa_free_attrib_data(struct gl_context * ctx)1490 _mesa_free_attrib_data(struct gl_context *ctx)
1491 {
1492    for (unsigned i = 0; i < ARRAY_SIZE(ctx->AttribStack); i++)
1493       FREE(ctx->AttribStack[i]);
1494 }
1495 
1496 
1497 void
_mesa_init_attrib(struct gl_context * ctx)1498 _mesa_init_attrib(struct gl_context *ctx)
1499 {
1500    /* Renderer and client attribute stacks */
1501    ctx->AttribStackDepth = 0;
1502    ctx->ClientAttribStackDepth = 0;
1503 }
1504