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