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