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