Sync with trunk r63637.
[reactos.git] / dll / opengl / 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 <precomp.h>
27
28 /**
29 * glEnable()/glDisable() attribute group (GL_ENABLE_BIT).
30 */
31 struct gl_enable_attrib
32 {
33 GLboolean AlphaTest;
34 GLboolean AutoNormal;
35 GLboolean Blend;
36 GLbitfield ClipPlanes;
37 GLboolean ColorMaterial;
38 GLboolean CullFace;
39 GLboolean DepthTest;
40 GLboolean Dither;
41 GLboolean Fog;
42 GLboolean Light[MAX_LIGHTS];
43 GLboolean Lighting;
44 GLboolean LineSmooth;
45 GLboolean LineStipple;
46 GLboolean IndexLogicOp;
47 GLboolean ColorLogicOp;
48
49 GLboolean Map1Color4;
50 GLboolean Map1Index;
51 GLboolean Map1Normal;
52 GLboolean Map1TextureCoord1;
53 GLboolean Map1TextureCoord2;
54 GLboolean Map1TextureCoord3;
55 GLboolean Map1TextureCoord4;
56 GLboolean Map1Vertex3;
57 GLboolean Map1Vertex4;
58 GLboolean Map2Color4;
59 GLboolean Map2Index;
60 GLboolean Map2Normal;
61 GLboolean Map2TextureCoord1;
62 GLboolean Map2TextureCoord2;
63 GLboolean Map2TextureCoord3;
64 GLboolean Map2TextureCoord4;
65 GLboolean Map2Vertex3;
66 GLboolean Map2Vertex4;
67
68 GLboolean Normalize;
69 GLboolean PixelTexture;
70 GLboolean PointSmooth;
71 GLboolean PolygonOffsetPoint;
72 GLboolean PolygonOffsetLine;
73 GLboolean PolygonOffsetFill;
74 GLboolean PolygonSmooth;
75 GLboolean PolygonStipple;
76 GLboolean RescaleNormals;
77 GLboolean Scissor;
78 GLboolean Stencil;
79 GLboolean MultisampleEnabled; /* GL_ARB_multisample */
80 GLboolean SampleAlphaToCoverage; /* GL_ARB_multisample */
81 GLboolean SampleAlphaToOne; /* GL_ARB_multisample */
82 GLboolean SampleCoverage; /* GL_ARB_multisample */
83 GLboolean RasterPositionUnclipped; /* GL_IBM_rasterpos_clip */
84
85 GLbitfield Texture;
86 GLbitfield TexGen;
87
88 /* GL_ARB_point_sprite / GL_NV_point_sprite */
89 GLboolean PointSprite;
90 };
91
92
93 /**
94 * Node for the attribute stack.
95 */
96 struct gl_attrib_node
97 {
98 GLbitfield kind;
99 void *data;
100 struct gl_attrib_node *next;
101 };
102
103
104
105 /**
106 * Special struct for saving/restoring texture state (GL_TEXTURE_BIT)
107 */
108 struct texture_state
109 {
110 struct gl_texture_attrib Texture; /**< The usual context state */
111
112 /** to save per texture object state (wrap modes, filters, etc): */
113 struct gl_texture_object SavedObj[NUM_TEXTURE_TARGETS];
114
115 /**
116 * To save references to texture objects (so they don't get accidentally
117 * deleted while saved in the attribute stack).
118 */
119 struct gl_texture_object *SavedTexRef[NUM_TEXTURE_TARGETS];
120
121 /* We need to keep a reference to the shared state. That's where the
122 * default texture objects are kept. We don't want that state to be
123 * freed while the attribute stack contains pointers to any default
124 * texture objects.
125 */
126 struct gl_shared_state *SharedRef;
127 };
128
129
130 #if FEATURE_attrib_stack
131
132
133 /**
134 * Allocate new attribute node of given type/kind. Attach payload data.
135 * Insert it into the linked list named by 'head'.
136 */
137 static void
138 save_attrib_data(struct gl_attrib_node **head,
139 GLbitfield kind, void *payload)
140 {
141 struct gl_attrib_node *n = MALLOC_STRUCT(gl_attrib_node);
142 if (n) {
143 n->kind = kind;
144 n->data = payload;
145 /* insert at head */
146 n->next = *head;
147 *head = n;
148 }
149 else {
150 /* out of memory! */
151 }
152 }
153
154
155 void GLAPIENTRY
156 _mesa_PushAttrib(GLbitfield mask)
157 {
158 struct gl_attrib_node *head;
159
160 GET_CURRENT_CONTEXT(ctx);
161 ASSERT_OUTSIDE_BEGIN_END(ctx);
162
163 if (MESA_VERBOSE & VERBOSE_API)
164 _mesa_debug(ctx, "glPushAttrib %x\n", (int) mask);
165
166 if (ctx->AttribStackDepth >= MAX_ATTRIB_STACK_DEPTH) {
167 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushAttrib" );
168 return;
169 }
170
171 /* Build linked list of attribute nodes which save all attribute */
172 /* groups specified by the mask. */
173 head = NULL;
174
175 if (mask & GL_ACCUM_BUFFER_BIT) {
176 struct gl_accum_attrib *attr;
177 attr = MALLOC_STRUCT( gl_accum_attrib );
178 memcpy( attr, &ctx->Accum, sizeof(struct gl_accum_attrib) );
179 save_attrib_data(&head, GL_ACCUM_BUFFER_BIT, attr);
180 }
181
182 if (mask & GL_COLOR_BUFFER_BIT) {
183 struct gl_colorbuffer_attrib *attr;
184 attr = MALLOC_STRUCT( gl_colorbuffer_attrib );
185 memcpy( attr, &ctx->Color, sizeof(struct gl_colorbuffer_attrib) );
186 /* push the Draw FBO's DrawBuffer[] state, not ctx->Color.DrawBuffer[] */
187 attr->DrawBuffer = ctx->DrawBuffer->ColorDrawBuffer;
188 save_attrib_data(&head, GL_COLOR_BUFFER_BIT, attr);
189 }
190
191 if (mask & GL_CURRENT_BIT) {
192 struct gl_current_attrib *attr;
193 FLUSH_CURRENT( ctx, 0 );
194 attr = MALLOC_STRUCT( gl_current_attrib );
195 memcpy( attr, &ctx->Current, sizeof(struct gl_current_attrib) );
196 save_attrib_data(&head, GL_CURRENT_BIT, attr);
197 }
198
199 if (mask & GL_DEPTH_BUFFER_BIT) {
200 struct gl_depthbuffer_attrib *attr;
201 attr = MALLOC_STRUCT( gl_depthbuffer_attrib );
202 memcpy( attr, &ctx->Depth, sizeof(struct gl_depthbuffer_attrib) );
203 save_attrib_data(&head, GL_DEPTH_BUFFER_BIT, attr);
204 }
205
206 if (mask & GL_ENABLE_BIT) {
207 struct gl_enable_attrib *attr;
208 GLuint i;
209 attr = MALLOC_STRUCT( gl_enable_attrib );
210 /* Copy enable flags from all other attributes into the enable struct. */
211 attr->AlphaTest = ctx->Color.AlphaEnabled;
212 attr->AutoNormal = ctx->Eval.AutoNormal;
213 attr->Blend = ctx->Color.BlendEnabled;
214 attr->ClipPlanes = ctx->Transform.ClipPlanesEnabled;
215 attr->ColorMaterial = ctx->Light.ColorMaterialEnabled;
216 attr->CullFace = ctx->Polygon.CullFlag;
217 attr->DepthTest = ctx->Depth.Test;
218 attr->Dither = ctx->Color.DitherFlag;
219 attr->Fog = ctx->Fog.Enabled;
220 for (i = 0; i < ctx->Const.MaxLights; i++) {
221 attr->Light[i] = ctx->Light.Light[i].Enabled;
222 }
223 attr->Lighting = ctx->Light.Enabled;
224 attr->LineSmooth = ctx->Line.SmoothFlag;
225 attr->LineStipple = ctx->Line.StippleFlag;
226 attr->IndexLogicOp = ctx->Color.IndexLogicOpEnabled;
227 attr->ColorLogicOp = ctx->Color.ColorLogicOpEnabled;
228 attr->Map1Color4 = ctx->Eval.Map1Color4;
229 attr->Map1Index = ctx->Eval.Map1Index;
230 attr->Map1Normal = ctx->Eval.Map1Normal;
231 attr->Map1TextureCoord1 = ctx->Eval.Map1TextureCoord1;
232 attr->Map1TextureCoord2 = ctx->Eval.Map1TextureCoord2;
233 attr->Map1TextureCoord3 = ctx->Eval.Map1TextureCoord3;
234 attr->Map1TextureCoord4 = ctx->Eval.Map1TextureCoord4;
235 attr->Map1Vertex3 = ctx->Eval.Map1Vertex3;
236 attr->Map1Vertex4 = ctx->Eval.Map1Vertex4;
237 attr->Map2Color4 = ctx->Eval.Map2Color4;
238 attr->Map2Index = ctx->Eval.Map2Index;
239 attr->Map2Normal = ctx->Eval.Map2Normal;
240 attr->Map2TextureCoord1 = ctx->Eval.Map2TextureCoord1;
241 attr->Map2TextureCoord2 = ctx->Eval.Map2TextureCoord2;
242 attr->Map2TextureCoord3 = ctx->Eval.Map2TextureCoord3;
243 attr->Map2TextureCoord4 = ctx->Eval.Map2TextureCoord4;
244 attr->Map2Vertex3 = ctx->Eval.Map2Vertex3;
245 attr->Map2Vertex4 = ctx->Eval.Map2Vertex4;
246 attr->Normalize = ctx->Transform.Normalize;
247 attr->RasterPositionUnclipped = ctx->Transform.RasterPositionUnclipped;
248 attr->PointSmooth = ctx->Point.SmoothFlag;
249 attr->PointSprite = ctx->Point.PointSprite;
250 attr->PolygonOffsetPoint = ctx->Polygon.OffsetPoint;
251 attr->PolygonOffsetLine = ctx->Polygon.OffsetLine;
252 attr->PolygonOffsetFill = ctx->Polygon.OffsetFill;
253 attr->PolygonSmooth = ctx->Polygon.SmoothFlag;
254 attr->PolygonStipple = ctx->Polygon.StippleFlag;
255 attr->RescaleNormals = ctx->Transform.RescaleNormals;
256 attr->Scissor = ctx->Scissor.Enabled;
257 attr->Stencil = ctx->Stencil.Enabled;
258 attr->MultisampleEnabled = ctx->Multisample.Enabled;
259 attr->SampleAlphaToCoverage = ctx->Multisample.SampleAlphaToCoverage;
260 attr->SampleAlphaToOne = ctx->Multisample.SampleAlphaToOne;
261 attr->SampleCoverage = ctx->Multisample.SampleCoverage;
262 attr->Texture = ctx->Texture.Unit.Enabled;
263 attr->TexGen = ctx->Texture.Unit.TexGenEnabled;
264 save_attrib_data(&head, GL_ENABLE_BIT, attr);
265 }
266
267 if (mask & GL_EVAL_BIT) {
268 struct gl_eval_attrib *attr;
269 attr = MALLOC_STRUCT( gl_eval_attrib );
270 memcpy( attr, &ctx->Eval, sizeof(struct gl_eval_attrib) );
271 save_attrib_data(&head, GL_EVAL_BIT, attr);
272 }
273
274 if (mask & GL_FOG_BIT) {
275 struct gl_fog_attrib *attr;
276 attr = MALLOC_STRUCT( gl_fog_attrib );
277 memcpy( attr, &ctx->Fog, sizeof(struct gl_fog_attrib) );
278 save_attrib_data(&head, GL_FOG_BIT, attr);
279 }
280
281 if (mask & GL_HINT_BIT) {
282 struct gl_hint_attrib *attr;
283 attr = MALLOC_STRUCT( gl_hint_attrib );
284 memcpy( attr, &ctx->Hint, sizeof(struct gl_hint_attrib) );
285 save_attrib_data(&head, GL_HINT_BIT, attr);
286 }
287
288 if (mask & GL_LIGHTING_BIT) {
289 struct gl_light_attrib *attr;
290 FLUSH_CURRENT(ctx, 0); /* flush material changes */
291 attr = MALLOC_STRUCT( gl_light_attrib );
292 memcpy( attr, &ctx->Light, sizeof(struct gl_light_attrib) );
293 save_attrib_data(&head, GL_LIGHTING_BIT, attr);
294 }
295
296 if (mask & GL_LINE_BIT) {
297 struct gl_line_attrib *attr;
298 attr = MALLOC_STRUCT( gl_line_attrib );
299 memcpy( attr, &ctx->Line, sizeof(struct gl_line_attrib) );
300 save_attrib_data(&head, GL_LINE_BIT, attr);
301 }
302
303 if (mask & GL_LIST_BIT) {
304 struct gl_list_attrib *attr;
305 attr = MALLOC_STRUCT( gl_list_attrib );
306 memcpy( attr, &ctx->List, sizeof(struct gl_list_attrib) );
307 save_attrib_data(&head, GL_LIST_BIT, attr);
308 }
309
310 if (mask & GL_PIXEL_MODE_BIT) {
311 struct gl_pixel_attrib *attr;
312 attr = MALLOC_STRUCT( gl_pixel_attrib );
313 memcpy( attr, &ctx->Pixel, sizeof(struct gl_pixel_attrib) );
314 /* push the Read FBO's ReadBuffer state, not ctx->Pixel.ReadBuffer */
315 attr->ReadBuffer = ctx->ReadBuffer->ColorReadBuffer;
316 save_attrib_data(&head, GL_PIXEL_MODE_BIT, attr);
317 }
318
319 if (mask & GL_POINT_BIT) {
320 struct gl_point_attrib *attr;
321 attr = MALLOC_STRUCT( gl_point_attrib );
322 memcpy( attr, &ctx->Point, sizeof(struct gl_point_attrib) );
323 save_attrib_data(&head, GL_POINT_BIT, attr);
324 }
325
326 if (mask & GL_POLYGON_BIT) {
327 struct gl_polygon_attrib *attr;
328 attr = MALLOC_STRUCT( gl_polygon_attrib );
329 memcpy( attr, &ctx->Polygon, sizeof(struct gl_polygon_attrib) );
330 save_attrib_data(&head, GL_POLYGON_BIT, attr);
331 }
332
333 if (mask & GL_POLYGON_STIPPLE_BIT) {
334 GLuint *stipple;
335 stipple = (GLuint *) MALLOC( 32*sizeof(GLuint) );
336 memcpy( stipple, ctx->PolygonStipple, 32*sizeof(GLuint) );
337 save_attrib_data(&head, GL_POLYGON_STIPPLE_BIT, stipple);
338 }
339
340 if (mask & GL_SCISSOR_BIT) {
341 struct gl_scissor_attrib *attr;
342 attr = MALLOC_STRUCT( gl_scissor_attrib );
343 memcpy( attr, &ctx->Scissor, sizeof(struct gl_scissor_attrib) );
344 save_attrib_data(&head, GL_SCISSOR_BIT, attr);
345 }
346
347 if (mask & GL_STENCIL_BUFFER_BIT) {
348 struct gl_stencil_attrib *attr;
349 attr = MALLOC_STRUCT( gl_stencil_attrib );
350 memcpy( attr, &ctx->Stencil, sizeof(struct gl_stencil_attrib) );
351 save_attrib_data(&head, GL_STENCIL_BUFFER_BIT, attr);
352 }
353
354 if (mask & GL_TEXTURE_BIT) {
355 struct texture_state *texstate = CALLOC_STRUCT(texture_state);
356 GLuint tex;
357
358 if (!texstate) {
359 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib(GL_TEXTURE_BIT)");
360 goto end;
361 }
362
363 _mesa_lock_context_textures(ctx);
364
365 /* copy/save the bulk of texture state here */
366 memcpy(&texstate->Texture, &ctx->Texture, sizeof(ctx->Texture));
367
368 /* Save references to the currently bound texture objects so they don't
369 * accidentally get deleted while referenced in the attribute stack.
370 */
371 for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
372 _mesa_reference_texobj(&texstate->SavedTexRef[tex],
373 ctx->Texture.Unit.CurrentTex[tex]);
374 }
375
376 /* copy state/contents of the currently bound texture objects */
377 for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
378 _mesa_copy_texture_object(&texstate->SavedObj[tex],
379 ctx->Texture.Unit.CurrentTex[tex]);
380 }
381
382 _mesa_reference_shared_state(ctx, &texstate->SharedRef, ctx->Shared);
383
384 _mesa_unlock_context_textures(ctx);
385
386 save_attrib_data(&head, GL_TEXTURE_BIT, texstate);
387 }
388
389 if (mask & GL_TRANSFORM_BIT) {
390 struct gl_transform_attrib *attr;
391 attr = MALLOC_STRUCT( gl_transform_attrib );
392 memcpy( attr, &ctx->Transform, sizeof(struct gl_transform_attrib) );
393 save_attrib_data(&head, GL_TRANSFORM_BIT, attr);
394 }
395
396 if (mask & GL_VIEWPORT_BIT) {
397 struct gl_viewport_attrib *attr;
398 attr = MALLOC_STRUCT( gl_viewport_attrib );
399 memcpy( attr, &ctx->Viewport, sizeof(struct gl_viewport_attrib) );
400 save_attrib_data(&head, GL_VIEWPORT_BIT, attr);
401 }
402
403 /* GL_ARB_multisample */
404 if (mask & GL_MULTISAMPLE_BIT_ARB) {
405 struct gl_multisample_attrib *attr;
406 attr = MALLOC_STRUCT( gl_multisample_attrib );
407 memcpy( attr, &ctx->Multisample, sizeof(struct gl_multisample_attrib) );
408 save_attrib_data(&head, GL_MULTISAMPLE_BIT_ARB, attr);
409 }
410
411 end:
412 ctx->AttribStack[ctx->AttribStackDepth] = head;
413 ctx->AttribStackDepth++;
414 }
415
416
417
418 static void
419 pop_enable_group(struct gl_context *ctx, const struct gl_enable_attrib *enable)
420 {
421 GLuint i;
422
423 #define TEST_AND_UPDATE(VALUE, NEWVALUE, ENUM) \
424 if ((VALUE) != (NEWVALUE)) { \
425 _mesa_set_enable( ctx, ENUM, (NEWVALUE) ); \
426 }
427
428 TEST_AND_UPDATE(ctx->Color.AlphaEnabled, enable->AlphaTest, GL_ALPHA_TEST);
429 if (ctx->Color.BlendEnabled != enable->Blend) {
430 _mesa_set_enable(ctx, GL_BLEND, (enable->Blend & 1));
431 }
432
433 for (i=0;i<ctx->Const.MaxClipPlanes;i++) {
434 const GLuint mask = 1 << i;
435 if ((ctx->Transform.ClipPlanesEnabled & mask) != (enable->ClipPlanes & mask))
436 _mesa_set_enable(ctx, (GLenum) (GL_CLIP_PLANE0 + i),
437 !!(enable->ClipPlanes & mask));
438 }
439
440 TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled, enable->ColorMaterial,
441 GL_COLOR_MATERIAL);
442 TEST_AND_UPDATE(ctx->Polygon.CullFlag, enable->CullFace, GL_CULL_FACE);
443 TEST_AND_UPDATE(ctx->Depth.Test, enable->DepthTest, GL_DEPTH_TEST);
444 TEST_AND_UPDATE(ctx->Color.DitherFlag, enable->Dither, GL_DITHER);
445 TEST_AND_UPDATE(ctx->Fog.Enabled, enable->Fog, GL_FOG);
446 TEST_AND_UPDATE(ctx->Light.Enabled, enable->Lighting, GL_LIGHTING);
447 TEST_AND_UPDATE(ctx->Line.SmoothFlag, enable->LineSmooth, GL_LINE_SMOOTH);
448 TEST_AND_UPDATE(ctx->Line.StippleFlag, enable->LineStipple,
449 GL_LINE_STIPPLE);
450 TEST_AND_UPDATE(ctx->Color.IndexLogicOpEnabled, enable->IndexLogicOp,
451 GL_INDEX_LOGIC_OP);
452 TEST_AND_UPDATE(ctx->Color.ColorLogicOpEnabled, enable->ColorLogicOp,
453 GL_COLOR_LOGIC_OP);
454
455 TEST_AND_UPDATE(ctx->Eval.Map1Color4, enable->Map1Color4, GL_MAP1_COLOR_4);
456 TEST_AND_UPDATE(ctx->Eval.Map1Index, enable->Map1Index, GL_MAP1_INDEX);
457 TEST_AND_UPDATE(ctx->Eval.Map1Normal, enable->Map1Normal, GL_MAP1_NORMAL);
458 TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord1, enable->Map1TextureCoord1,
459 GL_MAP1_TEXTURE_COORD_1);
460 TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord2, enable->Map1TextureCoord2,
461 GL_MAP1_TEXTURE_COORD_2);
462 TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord3, enable->Map1TextureCoord3,
463 GL_MAP1_TEXTURE_COORD_3);
464 TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord4, enable->Map1TextureCoord4,
465 GL_MAP1_TEXTURE_COORD_4);
466 TEST_AND_UPDATE(ctx->Eval.Map1Vertex3, enable->Map1Vertex3,
467 GL_MAP1_VERTEX_3);
468 TEST_AND_UPDATE(ctx->Eval.Map1Vertex4, enable->Map1Vertex4,
469 GL_MAP1_VERTEX_4);
470
471 TEST_AND_UPDATE(ctx->Eval.Map2Color4, enable->Map2Color4, GL_MAP2_COLOR_4);
472 TEST_AND_UPDATE(ctx->Eval.Map2Index, enable->Map2Index, GL_MAP2_INDEX);
473 TEST_AND_UPDATE(ctx->Eval.Map2Normal, enable->Map2Normal, GL_MAP2_NORMAL);
474 TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord1, enable->Map2TextureCoord1,
475 GL_MAP2_TEXTURE_COORD_1);
476 TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord2, enable->Map2TextureCoord2,
477 GL_MAP2_TEXTURE_COORD_2);
478 TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord3, enable->Map2TextureCoord3,
479 GL_MAP2_TEXTURE_COORD_3);
480 TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord4, enable->Map2TextureCoord4,
481 GL_MAP2_TEXTURE_COORD_4);
482 TEST_AND_UPDATE(ctx->Eval.Map2Vertex3, enable->Map2Vertex3,
483 GL_MAP2_VERTEX_3);
484 TEST_AND_UPDATE(ctx->Eval.Map2Vertex4, enable->Map2Vertex4,
485 GL_MAP2_VERTEX_4);
486
487 TEST_AND_UPDATE(ctx->Eval.AutoNormal, enable->AutoNormal, GL_AUTO_NORMAL);
488 TEST_AND_UPDATE(ctx->Transform.Normalize, enable->Normalize, GL_NORMALIZE);
489 TEST_AND_UPDATE(ctx->Transform.RescaleNormals, enable->RescaleNormals,
490 GL_RESCALE_NORMAL_EXT);
491 TEST_AND_UPDATE(ctx->Transform.RasterPositionUnclipped,
492 enable->RasterPositionUnclipped,
493 GL_RASTER_POSITION_UNCLIPPED_IBM);
494 TEST_AND_UPDATE(ctx->Point.SmoothFlag, enable->PointSmooth,
495 GL_POINT_SMOOTH);
496 if (ctx->Extensions.NV_point_sprite || ctx->Extensions.ARB_point_sprite) {
497 TEST_AND_UPDATE(ctx->Point.PointSprite, enable->PointSprite,
498 GL_POINT_SPRITE_NV);
499 }
500 TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, enable->PolygonOffsetPoint,
501 GL_POLYGON_OFFSET_POINT);
502 TEST_AND_UPDATE(ctx->Polygon.OffsetLine, enable->PolygonOffsetLine,
503 GL_POLYGON_OFFSET_LINE);
504 TEST_AND_UPDATE(ctx->Polygon.OffsetFill, enable->PolygonOffsetFill,
505 GL_POLYGON_OFFSET_FILL);
506 TEST_AND_UPDATE(ctx->Polygon.SmoothFlag, enable->PolygonSmooth,
507 GL_POLYGON_SMOOTH);
508 TEST_AND_UPDATE(ctx->Polygon.StippleFlag, enable->PolygonStipple,
509 GL_POLYGON_STIPPLE);
510 TEST_AND_UPDATE(ctx->Scissor.Enabled, enable->Scissor, GL_SCISSOR_TEST);
511 TEST_AND_UPDATE(ctx->Stencil.Enabled, enable->Stencil, GL_STENCIL_TEST);
512 TEST_AND_UPDATE(ctx->Multisample.Enabled, enable->MultisampleEnabled,
513 GL_MULTISAMPLE_ARB);
514 TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
515 enable->SampleAlphaToCoverage,
516 GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
517 TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
518 enable->SampleAlphaToOne,
519 GL_SAMPLE_ALPHA_TO_ONE_ARB);
520 TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
521 enable->SampleCoverage,
522 GL_SAMPLE_COVERAGE_ARB);
523
524 /* texture unit enables */
525 {
526 const GLbitfield enabled = enable->Texture;
527 const GLbitfield genEnabled = enable->TexGen;
528
529 if (ctx->Texture.Unit.Enabled != enabled) {
530 _mesa_set_enable(ctx, GL_TEXTURE_1D, !!(enabled & TEXTURE_1D_BIT));
531 _mesa_set_enable(ctx, GL_TEXTURE_2D, !!(enabled & TEXTURE_2D_BIT));
532 if (ctx->Extensions.ARB_texture_cube_map) {
533 _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP,
534 !!(enabled & TEXTURE_CUBE_BIT));
535 }
536 }
537
538 if (ctx->Texture.Unit.TexGenEnabled != genEnabled) {
539 _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, !!(genEnabled & S_BIT));
540 _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, !!(genEnabled & T_BIT));
541 _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, !!(genEnabled & R_BIT));
542 _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, !!(genEnabled & Q_BIT));
543 }
544 }
545 }
546
547
548 /**
549 * Pop/restore texture attribute/group state.
550 */
551 static void
552 pop_texture_group(struct gl_context *ctx, struct texture_state *texstate)
553 {
554 const struct gl_texture_unit *unit;
555 GLuint tgt;
556
557 _mesa_lock_context_textures(ctx);
558
559 unit = &texstate->Texture.Unit;
560
561 _mesa_set_enable(ctx, GL_TEXTURE_1D, !!(unit->Enabled & TEXTURE_1D_BIT));
562 _mesa_set_enable(ctx, GL_TEXTURE_2D, !!(unit->Enabled & TEXTURE_2D_BIT));
563
564 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->EnvMode);
565 _mesa_TexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, unit->EnvColor);
566 _mesa_TexGeni(GL_S, GL_TEXTURE_GEN_MODE, unit->GenS.Mode);
567 _mesa_TexGeni(GL_T, GL_TEXTURE_GEN_MODE, unit->GenT.Mode);
568 _mesa_TexGeni(GL_R, GL_TEXTURE_GEN_MODE, unit->GenR.Mode);
569 _mesa_TexGeni(GL_Q, GL_TEXTURE_GEN_MODE, unit->GenQ.Mode);
570 _mesa_TexGenfv(GL_S, GL_OBJECT_PLANE, unit->GenS.ObjectPlane);
571 _mesa_TexGenfv(GL_T, GL_OBJECT_PLANE, unit->GenT.ObjectPlane);
572 _mesa_TexGenfv(GL_R, GL_OBJECT_PLANE, unit->GenR.ObjectPlane);
573 _mesa_TexGenfv(GL_Q, GL_OBJECT_PLANE, unit->GenQ.ObjectPlane);
574 /* Eye plane done differently to avoid re-transformation */
575 {
576 struct gl_texture_unit *destUnit = &ctx->Texture.Unit;
577 COPY_4FV(destUnit->GenS.EyePlane, unit->GenS.EyePlane);
578 COPY_4FV(destUnit->GenT.EyePlane, unit->GenT.EyePlane);
579 COPY_4FV(destUnit->GenR.EyePlane, unit->GenR.EyePlane);
580 COPY_4FV(destUnit->GenQ.EyePlane, unit->GenQ.EyePlane);
581 if (ctx->Driver.TexGen) {
582 ctx->Driver.TexGen(ctx, GL_S, GL_EYE_PLANE, unit->GenS.EyePlane);
583 ctx->Driver.TexGen(ctx, GL_T, GL_EYE_PLANE, unit->GenT.EyePlane);
584 ctx->Driver.TexGen(ctx, GL_R, GL_EYE_PLANE, unit->GenR.EyePlane);
585 ctx->Driver.TexGen(ctx, GL_Q, GL_EYE_PLANE, unit->GenQ.EyePlane);
586 }
587 }
588 _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, !!(unit->TexGenEnabled & S_BIT));
589 _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, !!(unit->TexGenEnabled & T_BIT));
590 _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, !!(unit->TexGenEnabled & R_BIT));
591 _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, !!(unit->TexGenEnabled & Q_BIT));
592 _mesa_TexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS,
593 unit->LodBias);
594 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,
595 unit->Combine.ModeRGB);
596 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,
597 unit->Combine.ModeA);
598 {
599 const GLuint n = ctx->Extensions.NV_texture_env_combine4 ? 4 : 3;
600 GLuint i;
601 for (i = 0; i < n; i++) {
602 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB + i,
603 unit->Combine.SourceRGB[i]);
604 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA + i,
605 unit->Combine.SourceA[i]);
606 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB + i,
607 unit->Combine.OperandRGB[i]);
608 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA + i,
609 unit->Combine.OperandA[i]);
610 }
611 }
612 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE, 1 << unit->Combine.ScaleShiftRGB);
613 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1 << unit->Combine.ScaleShiftA);
614
615 /* Restore texture object state for each target */
616 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
617 const struct gl_texture_object *obj = NULL;
618 const struct gl_sampler_object *samp;
619 GLenum target;
620
621 obj = &texstate->SavedObj[tgt];
622
623 /* don't restore state for unsupported targets to prevent
624 * raising GL errors.
625 */
626 if (obj->Target == GL_TEXTURE_CUBE_MAP_ARB &&
627 !ctx->Extensions.ARB_texture_cube_map) {
628 continue;
629 }
630
631 target = obj->Target;
632
633 _mesa_BindTexture(target, obj->Name);
634
635 samp = &obj->Sampler;
636
637 _mesa_TexParameterfv(target, GL_TEXTURE_BORDER_COLOR, samp->BorderColor.f);
638 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, samp->WrapS);
639 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, samp->WrapT);
640 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, samp->WrapR);
641 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, samp->MinFilter);
642 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, samp->MagFilter);
643 _mesa_TexParameterf(target, GL_TEXTURE_PRIORITY, obj->Priority);
644 if (ctx->Extensions.EXT_texture_filter_anisotropic) {
645 _mesa_TexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
646 samp->MaxAnisotropy);
647 }
648 }
649
650 /* remove saved references to the texture objects */
651 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
652 _mesa_reference_texobj(&texstate->SavedTexRef[tgt], NULL);
653 }
654
655 _mesa_reference_shared_state(ctx, &texstate->SharedRef, NULL);
656
657 _mesa_unlock_context_textures(ctx);
658 }
659
660
661 /*
662 * This function is kind of long just because we have to call a lot
663 * of device driver functions to update device driver state.
664 *
665 * XXX As it is now, most of the pop-code calls immediate-mode Mesa functions
666 * in order to restore GL state. This isn't terribly efficient but it
667 * ensures that dirty flags and any derived state gets updated correctly.
668 * We could at least check if the value to restore equals the current value
669 * and then skip the Mesa call.
670 */
671 void GLAPIENTRY
672 _mesa_PopAttrib(void)
673 {
674 struct gl_attrib_node *attr, *next;
675 GET_CURRENT_CONTEXT(ctx);
676 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
677
678 if (ctx->AttribStackDepth == 0) {
679 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopAttrib" );
680 return;
681 }
682
683 ctx->AttribStackDepth--;
684 attr = ctx->AttribStack[ctx->AttribStackDepth];
685
686 while (attr) {
687
688 if (MESA_VERBOSE & VERBOSE_API) {
689 _mesa_debug(ctx, "glPopAttrib %s\n",
690 _mesa_lookup_enum_by_nr(attr->kind));
691 }
692
693 switch (attr->kind) {
694 case GL_ACCUM_BUFFER_BIT:
695 {
696 const struct gl_accum_attrib *accum;
697 accum = (const struct gl_accum_attrib *) attr->data;
698 _mesa_ClearAccum(accum->ClearColor[0],
699 accum->ClearColor[1],
700 accum->ClearColor[2],
701 accum->ClearColor[3]);
702 }
703 break;
704 case GL_COLOR_BUFFER_BIT:
705 {
706 const struct gl_colorbuffer_attrib *color;
707
708 color = (const struct gl_colorbuffer_attrib *) attr->data;
709 _mesa_ClearIndex((GLfloat) color->ClearIndex);
710 _mesa_ClearColor(color->ClearColor.f[0],
711 color->ClearColor.f[1],
712 color->ClearColor.f[2],
713 color->ClearColor.f[3]);
714 _mesa_IndexMask(color->IndexMask);
715 _mesa_ColorMask((GLboolean) (color->ColorMask[0] != 0),
716 (GLboolean) (color->ColorMask[1] != 0),
717 (GLboolean) (color->ColorMask[2] != 0),
718 (GLboolean) (color->ColorMask[3] != 0));
719 _mesa_DrawBuffer(color->DrawBuffer);
720 _mesa_set_enable(ctx, GL_ALPHA_TEST, color->AlphaEnabled);
721 _mesa_AlphaFunc(color->AlphaFunc, color->AlphaRef);
722 if (ctx->Color.BlendEnabled != color->BlendEnabled) {
723 _mesa_set_enable(ctx, GL_BLEND, (color->BlendEnabled & 1));
724 }
725 /* set same blend modes for all buffers */
726 _mesa_BlendFunc(color->SrcFactor, color->DstFactor);
727 _mesa_LogicOp(color->LogicOp);
728 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP,
729 color->ColorLogicOpEnabled);
730 _mesa_set_enable(ctx, GL_INDEX_LOGIC_OP,
731 color->IndexLogicOpEnabled);
732 _mesa_set_enable(ctx, GL_DITHER, color->DitherFlag);
733 }
734 break;
735 case GL_CURRENT_BIT:
736 FLUSH_CURRENT( ctx, 0 );
737 memcpy( &ctx->Current, attr->data,
738 sizeof(struct gl_current_attrib) );
739 break;
740 case GL_DEPTH_BUFFER_BIT:
741 {
742 const struct gl_depthbuffer_attrib *depth;
743 depth = (const struct gl_depthbuffer_attrib *) attr->data;
744 _mesa_DepthFunc(depth->Func);
745 _mesa_ClearDepth(depth->Clear);
746 _mesa_set_enable(ctx, GL_DEPTH_TEST, depth->Test);
747 _mesa_DepthMask(depth->Mask);
748 }
749 break;
750 case GL_ENABLE_BIT:
751 {
752 const struct gl_enable_attrib *enable;
753 enable = (const struct gl_enable_attrib *) attr->data;
754 pop_enable_group(ctx, enable);
755 ctx->NewState |= _NEW_ALL;
756 }
757 break;
758 case GL_EVAL_BIT:
759 memcpy( &ctx->Eval, attr->data, sizeof(struct gl_eval_attrib) );
760 ctx->NewState |= _NEW_EVAL;
761 break;
762 case GL_FOG_BIT:
763 {
764 const struct gl_fog_attrib *fog;
765 fog = (const struct gl_fog_attrib *) attr->data;
766 _mesa_set_enable(ctx, GL_FOG, fog->Enabled);
767 _mesa_Fogfv(GL_FOG_COLOR, fog->Color);
768 _mesa_Fogf(GL_FOG_DENSITY, fog->Density);
769 _mesa_Fogf(GL_FOG_START, fog->Start);
770 _mesa_Fogf(GL_FOG_END, fog->End);
771 _mesa_Fogf(GL_FOG_INDEX, fog->Index);
772 _mesa_Fogi(GL_FOG_MODE, fog->Mode);
773 }
774 break;
775 case GL_HINT_BIT:
776 {
777 const struct gl_hint_attrib *hint;
778 hint = (const struct gl_hint_attrib *) attr->data;
779 _mesa_Hint(GL_PERSPECTIVE_CORRECTION_HINT,
780 hint->PerspectiveCorrection );
781 _mesa_Hint(GL_POINT_SMOOTH_HINT, hint->PointSmooth);
782 _mesa_Hint(GL_LINE_SMOOTH_HINT, hint->LineSmooth);
783 _mesa_Hint(GL_POLYGON_SMOOTH_HINT, hint->PolygonSmooth);
784 _mesa_Hint(GL_FOG_HINT, hint->Fog);
785 _mesa_Hint(GL_CLIP_VOLUME_CLIPPING_HINT_EXT,
786 hint->ClipVolumeClipping);
787 }
788 break;
789 case GL_LIGHTING_BIT:
790 {
791 GLuint i;
792 const struct gl_light_attrib *light;
793 light = (const struct gl_light_attrib *) attr->data;
794 /* lighting enable */
795 _mesa_set_enable(ctx, GL_LIGHTING, light->Enabled);
796 /* per-light state */
797 if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
798 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
799
800 for (i = 0; i < ctx->Const.MaxLights; i++) {
801 const struct gl_light *l = &light->Light[i];
802 _mesa_set_enable(ctx, GL_LIGHT0 + i, l->Enabled);
803 _mesa_light(ctx, i, GL_AMBIENT, l->Ambient);
804 _mesa_light(ctx, i, GL_DIFFUSE, l->Diffuse);
805 _mesa_light(ctx, i, GL_SPECULAR, l->Specular );
806 _mesa_light(ctx, i, GL_POSITION, l->EyePosition);
807 _mesa_light(ctx, i, GL_SPOT_DIRECTION, l->SpotDirection);
808 {
809 GLfloat p[4] = { 0 };
810 p[0] = l->SpotExponent;
811 _mesa_light(ctx, i, GL_SPOT_EXPONENT, p);
812 }
813 {
814 GLfloat p[4] = { 0 };
815 p[0] = l->SpotCutoff;
816 _mesa_light(ctx, i, GL_SPOT_CUTOFF, p);
817 }
818 {
819 GLfloat p[4] = { 0 };
820 p[0] = l->ConstantAttenuation;
821 _mesa_light(ctx, i, GL_CONSTANT_ATTENUATION, p);
822 }
823 {
824 GLfloat p[4] = { 0 };
825 p[0] = l->LinearAttenuation;
826 _mesa_light(ctx, i, GL_LINEAR_ATTENUATION, p);
827 }
828 {
829 GLfloat p[4] = { 0 };
830 p[0] = l->QuadraticAttenuation;
831 _mesa_light(ctx, i, GL_QUADRATIC_ATTENUATION, p);
832 }
833 }
834 /* light model */
835 _mesa_LightModelfv(GL_LIGHT_MODEL_AMBIENT,
836 light->Model.Ambient);
837 _mesa_LightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,
838 (GLfloat) light->Model.LocalViewer);
839 _mesa_LightModelf(GL_LIGHT_MODEL_TWO_SIDE,
840 (GLfloat) light->Model.TwoSide);
841 /* shade model */
842 _mesa_ShadeModel(light->ShadeModel);
843 /* color material */
844 _mesa_ColorMaterial(light->ColorMaterialFace,
845 light->ColorMaterialMode);
846 _mesa_set_enable(ctx, GL_COLOR_MATERIAL,
847 light->ColorMaterialEnabled);
848 /* materials */
849 memcpy(&ctx->Light.Material, &light->Material,
850 sizeof(struct gl_material));
851 }
852 break;
853 case GL_LINE_BIT:
854 {
855 const struct gl_line_attrib *line;
856 line = (const struct gl_line_attrib *) attr->data;
857 _mesa_set_enable(ctx, GL_LINE_SMOOTH, line->SmoothFlag);
858 _mesa_set_enable(ctx, GL_LINE_STIPPLE, line->StippleFlag);
859 _mesa_LineStipple(line->StippleFactor, line->StipplePattern);
860 _mesa_LineWidth(line->Width);
861 }
862 break;
863 case GL_LIST_BIT:
864 memcpy( &ctx->List, attr->data, sizeof(struct gl_list_attrib) );
865 break;
866 case GL_PIXEL_MODE_BIT:
867 memcpy( &ctx->Pixel, attr->data, sizeof(struct gl_pixel_attrib) );
868 /* XXX what other pixel state needs to be set by function calls? */
869 _mesa_ReadBuffer(ctx->Pixel.ReadBuffer);
870 ctx->NewState |= _NEW_PIXEL;
871 break;
872 case GL_POINT_BIT:
873 {
874 const struct gl_point_attrib *point;
875 point = (const struct gl_point_attrib *) attr->data;
876 _mesa_PointSize(point->Size);
877 _mesa_set_enable(ctx, GL_POINT_SMOOTH, point->SmoothFlag);
878 }
879 break;
880 case GL_POLYGON_BIT:
881 {
882 const struct gl_polygon_attrib *polygon;
883 polygon = (const struct gl_polygon_attrib *) attr->data;
884 _mesa_CullFace(polygon->CullFaceMode);
885 _mesa_FrontFace(polygon->FrontFace);
886 _mesa_PolygonMode(GL_FRONT, polygon->FrontMode);
887 _mesa_PolygonMode(GL_BACK, polygon->BackMode);
888 _mesa_PolygonOffset(polygon->OffsetFactor,
889 polygon->OffsetUnits);
890 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, polygon->SmoothFlag);
891 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, polygon->StippleFlag);
892 _mesa_set_enable(ctx, GL_CULL_FACE, polygon->CullFlag);
893 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_POINT,
894 polygon->OffsetPoint);
895 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_LINE,
896 polygon->OffsetLine);
897 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL,
898 polygon->OffsetFill);
899 }
900 break;
901 case GL_POLYGON_STIPPLE_BIT:
902 memcpy( ctx->PolygonStipple, attr->data, 32*sizeof(GLuint) );
903 ctx->NewState |= _NEW_POLYGONSTIPPLE;
904 if (ctx->Driver.PolygonStipple)
905 ctx->Driver.PolygonStipple( ctx, (const GLubyte *) attr->data );
906 break;
907 case GL_SCISSOR_BIT:
908 {
909 const struct gl_scissor_attrib *scissor;
910 scissor = (const struct gl_scissor_attrib *) attr->data;
911 _mesa_Scissor(scissor->X, scissor->Y,
912 scissor->Width, scissor->Height);
913 _mesa_set_enable(ctx, GL_SCISSOR_TEST, scissor->Enabled);
914 }
915 break;
916 case GL_STENCIL_BUFFER_BIT:
917 {
918 const struct gl_stencil_attrib *stencil;
919 stencil = (const struct gl_stencil_attrib *) attr->data;
920 _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
921 _mesa_ClearStencil(stencil->Clear);
922 _mesa_StencilFunc(stencil->Function,
923 stencil->Ref,
924 stencil->ValueMask);
925 _mesa_StencilMask(stencil->WriteMask);
926 _mesa_StencilOp(stencil->FailFunc,
927 stencil->ZFailFunc,
928 stencil->ZPassFunc);
929 }
930 break;
931 case GL_TRANSFORM_BIT:
932 {
933 GLuint i;
934 const struct gl_transform_attrib *xform;
935 xform = (const struct gl_transform_attrib *) attr->data;
936 _mesa_MatrixMode(xform->MatrixMode);
937 if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
938 _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
939
940 /* restore clip planes */
941 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
942 const GLuint mask = 1 << i;
943 const GLfloat *eyePlane = xform->EyeUserPlane[i];
944 COPY_4V(ctx->Transform.EyeUserPlane[i], eyePlane);
945 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i,
946 !!(xform->ClipPlanesEnabled & mask));
947 if (ctx->Driver.ClipPlane)
948 ctx->Driver.ClipPlane( ctx, GL_CLIP_PLANE0 + i, eyePlane );
949 }
950
951 /* normalize/rescale */
952 if (xform->Normalize != ctx->Transform.Normalize)
953 _mesa_set_enable(ctx, GL_NORMALIZE,ctx->Transform.Normalize);
954 if (xform->RescaleNormals != ctx->Transform.RescaleNormals)
955 _mesa_set_enable(ctx, GL_RESCALE_NORMAL_EXT,
956 ctx->Transform.RescaleNormals);
957 }
958 break;
959 case GL_TEXTURE_BIT:
960 /* Take care of texture object reference counters */
961 {
962 struct texture_state *texstate
963 = (struct texture_state *) attr->data;
964 pop_texture_group(ctx, texstate);
965 ctx->NewState |= _NEW_TEXTURE;
966 }
967 break;
968 case GL_VIEWPORT_BIT:
969 {
970 const struct gl_viewport_attrib *vp;
971 vp = (const struct gl_viewport_attrib *) attr->data;
972 _mesa_Viewport(vp->X, vp->Y, vp->Width, vp->Height);
973 _mesa_DepthRange(vp->Near, vp->Far);
974 }
975 break;
976 case GL_MULTISAMPLE_BIT_ARB:
977 {
978 const struct gl_multisample_attrib *ms;
979 ms = (const struct gl_multisample_attrib *) attr->data;
980
981 TEST_AND_UPDATE(ctx->Multisample.Enabled,
982 ms->Enabled,
983 GL_MULTISAMPLE);
984
985 TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
986 ms->SampleCoverage,
987 GL_SAMPLE_COVERAGE);
988
989 TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
990 ms->SampleAlphaToCoverage,
991 GL_SAMPLE_ALPHA_TO_COVERAGE);
992
993 TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
994 ms->SampleAlphaToOne,
995 GL_SAMPLE_ALPHA_TO_ONE);
996
997 _mesa_SampleCoverageARB(ms->SampleCoverageValue,
998 ms->SampleCoverageInvert);
999 }
1000 break;
1001
1002 default:
1003 _mesa_problem( ctx, "Bad attrib flag in PopAttrib");
1004 break;
1005 }
1006
1007 next = attr->next;
1008 FREE( attr->data );
1009 FREE( attr );
1010 attr = next;
1011 }
1012 }
1013
1014
1015 /**
1016 * Copy gl_pixelstore_attrib from src to dst, updating buffer
1017 * object refcounts.
1018 */
1019 static void
1020 copy_pixelstore(struct gl_context *ctx,
1021 struct gl_pixelstore_attrib *dst,
1022 const struct gl_pixelstore_attrib *src)
1023 {
1024 dst->Alignment = src->Alignment;
1025 dst->RowLength = src->RowLength;
1026 dst->SkipPixels = src->SkipPixels;
1027 dst->SkipRows = src->SkipRows;
1028 dst->ImageHeight = src->ImageHeight;
1029 dst->SkipImages = src->SkipImages;
1030 dst->SwapBytes = src->SwapBytes;
1031 dst->LsbFirst = src->LsbFirst;
1032 dst->Invert = src->Invert;
1033 }
1034
1035
1036 #define GL_CLIENT_PACK_BIT (1<<20)
1037 #define GL_CLIENT_UNPACK_BIT (1<<21)
1038
1039 /**
1040 * Copy gl_array_attrib from src to dest.
1041 * 'dest' must be in an initialized state.
1042 */
1043 static void
1044 copy_array_attrib(struct gl_context *ctx,
1045 struct gl_array_attrib *dest,
1046 struct gl_array_attrib *src)
1047 {
1048 GLuint i;
1049
1050 /* skip ArrayObj */
1051 /* skip DefaultArrayObj, Objects */
1052 dest->LockFirst = src->LockFirst;
1053 dest->LockCount = src->LockCount;
1054 /* skip NewState */
1055 /* skip RebindArrays */
1056
1057
1058 /* skip Name */
1059 /* skip RefCount */
1060
1061 for (i = 0; i < Elements(src->VertexAttrib); i++)
1062 _mesa_copy_client_array(ctx, &dest->VertexAttrib[i], &src->VertexAttrib[i]);
1063
1064 /* _Enabled must be the same than on push */
1065 dest->_Enabled = src->_Enabled;
1066 dest->_MaxElement = src->_MaxElement;
1067
1068 /* skip ArrayBufferObj */
1069 /* skip ElementArrayBufferObj */
1070 }
1071
1072 /**
1073 * Save the content of src to dest.
1074 */
1075 static void
1076 save_array_attrib(struct gl_context *ctx,
1077 struct gl_array_attrib *dest,
1078 struct gl_array_attrib *src)
1079 {
1080 /* And copy all of the rest. */
1081 copy_array_attrib(ctx, dest, src);
1082
1083 /* Just reference them here */
1084 _mesa_reference_buffer_object(ctx, &dest->ElementArrayBufferObj,
1085 src->ElementArrayBufferObj);
1086 }
1087
1088 /**
1089 * Restore the content of src to dest.
1090 */
1091 static void
1092 restore_array_attrib(struct gl_context *ctx,
1093 struct gl_array_attrib *dest,
1094 struct gl_array_attrib *src)
1095 {
1096 copy_array_attrib(ctx, dest, src);
1097
1098 if (src->ElementArrayBufferObj->Name == 0
1099 || _mesa_IsBufferARB(src->ElementArrayBufferObj->Name))
1100 _mesa_BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
1101 src->ElementArrayBufferObj->Name);
1102
1103 /* Better safe than sorry?! */
1104 dest->RebindArrays = GL_TRUE;
1105
1106 /* FIXME: Should some bits in ctx->Array->NewState also be set
1107 * FIXME: here? It seems like it should be set to inclusive-or
1108 * FIXME: of the old ArrayObj->_Enabled and the new _Enabled.
1109 * ... just do it.
1110 */
1111 dest->NewState |= src->_Enabled | dest->_Enabled;
1112 }
1113
1114
1115 void GLAPIENTRY
1116 _mesa_PushClientAttrib(GLbitfield mask)
1117 {
1118 struct gl_attrib_node *head;
1119
1120 GET_CURRENT_CONTEXT(ctx);
1121 ASSERT_OUTSIDE_BEGIN_END(ctx);
1122
1123 if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
1124 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushClientAttrib" );
1125 return;
1126 }
1127
1128 /* Build linked list of attribute nodes which save all attribute
1129 * groups specified by the mask.
1130 */
1131 head = NULL;
1132
1133 if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
1134 struct gl_pixelstore_attrib *attr;
1135 /* packing attribs */
1136 attr = CALLOC_STRUCT( gl_pixelstore_attrib );
1137 copy_pixelstore(ctx, attr, &ctx->Pack);
1138 save_attrib_data(&head, GL_CLIENT_PACK_BIT, attr);
1139 /* unpacking attribs */
1140 attr = CALLOC_STRUCT( gl_pixelstore_attrib );
1141 copy_pixelstore(ctx, attr, &ctx->Unpack);
1142 save_attrib_data(&head, GL_CLIENT_UNPACK_BIT, attr);
1143 }
1144
1145 if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1146 struct gl_array_attrib *attr;
1147 attr = CALLOC_STRUCT( gl_array_attrib );
1148 _mesa_init_varray(ctx, attr);
1149 save_array_attrib(ctx, attr, &ctx->Array);
1150 save_attrib_data(&head, GL_CLIENT_VERTEX_ARRAY_BIT, attr);
1151 }
1152
1153 ctx->ClientAttribStack[ctx->ClientAttribStackDepth] = head;
1154 ctx->ClientAttribStackDepth++;
1155 }
1156
1157
1158
1159
1160 void GLAPIENTRY
1161 _mesa_PopClientAttrib(void)
1162 {
1163 struct gl_attrib_node *node, *next;
1164
1165 GET_CURRENT_CONTEXT(ctx);
1166 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1167
1168 if (ctx->ClientAttribStackDepth == 0) {
1169 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib" );
1170 return;
1171 }
1172
1173 ctx->ClientAttribStackDepth--;
1174 node = ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
1175
1176 while (node) {
1177 switch (node->kind) {
1178 case GL_CLIENT_PACK_BIT:
1179 {
1180 struct gl_pixelstore_attrib *store =
1181 (struct gl_pixelstore_attrib *) node->data;
1182 copy_pixelstore(ctx, &ctx->Pack, store);
1183 }
1184 ctx->NewState |= _NEW_PACKUNPACK;
1185 break;
1186 case GL_CLIENT_UNPACK_BIT:
1187 {
1188 struct gl_pixelstore_attrib *store =
1189 (struct gl_pixelstore_attrib *) node->data;
1190 copy_pixelstore(ctx, &ctx->Unpack, store);
1191 }
1192 ctx->NewState |= _NEW_PACKUNPACK;
1193 break;
1194 case GL_CLIENT_VERTEX_ARRAY_BIT: {
1195 struct gl_array_attrib * attr =
1196 (struct gl_array_attrib *) node->data;
1197 restore_array_attrib(ctx, &ctx->Array, attr);
1198 _mesa_free_varray_data(ctx, attr);
1199 ctx->NewState |= _NEW_ARRAY;
1200 break;
1201 }
1202 default:
1203 _mesa_problem( ctx, "Bad attrib flag in PopClientAttrib");
1204 break;
1205 }
1206
1207 next = node->next;
1208 FREE( node->data );
1209 FREE( node );
1210 node = next;
1211 }
1212 }
1213
1214
1215 void
1216 _mesa_init_attrib_dispatch(struct _glapi_table *disp)
1217 {
1218 SET_PopAttrib(disp, _mesa_PopAttrib);
1219 SET_PushAttrib(disp, _mesa_PushAttrib);
1220 SET_PopClientAttrib(disp, _mesa_PopClientAttrib);
1221 SET_PushClientAttrib(disp, _mesa_PushClientAttrib);
1222 }
1223
1224
1225 #endif /* FEATURE_attrib_stack */
1226
1227
1228 /**
1229 * Free any attribute state data that might be attached to the context.
1230 */
1231 void
1232 _mesa_free_attrib_data(struct gl_context *ctx)
1233 {
1234 while (ctx->AttribStackDepth > 0) {
1235 struct gl_attrib_node *attr, *next;
1236
1237 ctx->AttribStackDepth--;
1238 attr = ctx->AttribStack[ctx->AttribStackDepth];
1239
1240 while (attr) {
1241 if (attr->kind == GL_TEXTURE_BIT) {
1242 struct texture_state *texstate = (struct texture_state*)attr->data;
1243 GLuint tgt;
1244 /* clear references to the saved texture objects */
1245 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
1246 _mesa_reference_texobj(&texstate->SavedTexRef[tgt], NULL);
1247 }
1248 _mesa_reference_shared_state(ctx, &texstate->SharedRef, NULL);
1249 }
1250 else {
1251 /* any other chunks of state that requires special handling? */
1252 }
1253
1254 next = attr->next;
1255 free(attr->data);
1256 free(attr);
1257 attr = next;
1258 }
1259 }
1260 }
1261
1262
1263 void _mesa_init_attrib( struct gl_context *ctx )
1264 {
1265 /* Renderer and client attribute stacks */
1266 ctx->AttribStackDepth = 0;
1267 ctx->ClientAttribStackDepth = 0;
1268 }