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