[OPENGL32/MESA]
[reactos.git] / reactos / 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_BlendFuncSeparateEXT(color->SrcRGB,
727 color->DstRGB,
728 color->SrcA,
729 color->DstA);
730 /* This special case is because glBlendEquationSeparateEXT
731 * cannot take GL_LOGIC_OP as a parameter.
732 */
733 if (color->EquationRGB == color->EquationA) {
734 _mesa_BlendEquation(color->EquationRGB);
735 }
736 else {
737 _mesa_BlendEquationSeparateEXT(
738 color->EquationRGB,
739 color->EquationA);
740 }
741 _mesa_BlendColor(color->BlendColor[0],
742 color->BlendColor[1],
743 color->BlendColor[2],
744 color->BlendColor[3]);
745 _mesa_LogicOp(color->LogicOp);
746 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP,
747 color->ColorLogicOpEnabled);
748 _mesa_set_enable(ctx, GL_INDEX_LOGIC_OP,
749 color->IndexLogicOpEnabled);
750 _mesa_set_enable(ctx, GL_DITHER, color->DitherFlag);
751 }
752 break;
753 case GL_CURRENT_BIT:
754 FLUSH_CURRENT( ctx, 0 );
755 memcpy( &ctx->Current, attr->data,
756 sizeof(struct gl_current_attrib) );
757 break;
758 case GL_DEPTH_BUFFER_BIT:
759 {
760 const struct gl_depthbuffer_attrib *depth;
761 depth = (const struct gl_depthbuffer_attrib *) attr->data;
762 _mesa_DepthFunc(depth->Func);
763 _mesa_ClearDepth(depth->Clear);
764 _mesa_set_enable(ctx, GL_DEPTH_TEST, depth->Test);
765 _mesa_DepthMask(depth->Mask);
766 }
767 break;
768 case GL_ENABLE_BIT:
769 {
770 const struct gl_enable_attrib *enable;
771 enable = (const struct gl_enable_attrib *) attr->data;
772 pop_enable_group(ctx, enable);
773 ctx->NewState |= _NEW_ALL;
774 }
775 break;
776 case GL_EVAL_BIT:
777 memcpy( &ctx->Eval, attr->data, sizeof(struct gl_eval_attrib) );
778 ctx->NewState |= _NEW_EVAL;
779 break;
780 case GL_FOG_BIT:
781 {
782 const struct gl_fog_attrib *fog;
783 fog = (const struct gl_fog_attrib *) attr->data;
784 _mesa_set_enable(ctx, GL_FOG, fog->Enabled);
785 _mesa_Fogfv(GL_FOG_COLOR, fog->Color);
786 _mesa_Fogf(GL_FOG_DENSITY, fog->Density);
787 _mesa_Fogf(GL_FOG_START, fog->Start);
788 _mesa_Fogf(GL_FOG_END, fog->End);
789 _mesa_Fogf(GL_FOG_INDEX, fog->Index);
790 _mesa_Fogi(GL_FOG_MODE, fog->Mode);
791 }
792 break;
793 case GL_HINT_BIT:
794 {
795 const struct gl_hint_attrib *hint;
796 hint = (const struct gl_hint_attrib *) attr->data;
797 _mesa_Hint(GL_PERSPECTIVE_CORRECTION_HINT,
798 hint->PerspectiveCorrection );
799 _mesa_Hint(GL_POINT_SMOOTH_HINT, hint->PointSmooth);
800 _mesa_Hint(GL_LINE_SMOOTH_HINT, hint->LineSmooth);
801 _mesa_Hint(GL_POLYGON_SMOOTH_HINT, hint->PolygonSmooth);
802 _mesa_Hint(GL_FOG_HINT, hint->Fog);
803 _mesa_Hint(GL_CLIP_VOLUME_CLIPPING_HINT_EXT,
804 hint->ClipVolumeClipping);
805 }
806 break;
807 case GL_LIGHTING_BIT:
808 {
809 GLuint i;
810 const struct gl_light_attrib *light;
811 light = (const struct gl_light_attrib *) attr->data;
812 /* lighting enable */
813 _mesa_set_enable(ctx, GL_LIGHTING, light->Enabled);
814 /* per-light state */
815 if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
816 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
817
818 for (i = 0; i < ctx->Const.MaxLights; i++) {
819 const struct gl_light *l = &light->Light[i];
820 _mesa_set_enable(ctx, GL_LIGHT0 + i, l->Enabled);
821 _mesa_light(ctx, i, GL_AMBIENT, l->Ambient);
822 _mesa_light(ctx, i, GL_DIFFUSE, l->Diffuse);
823 _mesa_light(ctx, i, GL_SPECULAR, l->Specular );
824 _mesa_light(ctx, i, GL_POSITION, l->EyePosition);
825 _mesa_light(ctx, i, GL_SPOT_DIRECTION, l->SpotDirection);
826 {
827 GLfloat p[4] = { 0 };
828 p[0] = l->SpotExponent;
829 _mesa_light(ctx, i, GL_SPOT_EXPONENT, p);
830 }
831 {
832 GLfloat p[4] = { 0 };
833 p[0] = l->SpotCutoff;
834 _mesa_light(ctx, i, GL_SPOT_CUTOFF, p);
835 }
836 {
837 GLfloat p[4] = { 0 };
838 p[0] = l->ConstantAttenuation;
839 _mesa_light(ctx, i, GL_CONSTANT_ATTENUATION, p);
840 }
841 {
842 GLfloat p[4] = { 0 };
843 p[0] = l->LinearAttenuation;
844 _mesa_light(ctx, i, GL_LINEAR_ATTENUATION, p);
845 }
846 {
847 GLfloat p[4] = { 0 };
848 p[0] = l->QuadraticAttenuation;
849 _mesa_light(ctx, i, GL_QUADRATIC_ATTENUATION, p);
850 }
851 }
852 /* light model */
853 _mesa_LightModelfv(GL_LIGHT_MODEL_AMBIENT,
854 light->Model.Ambient);
855 _mesa_LightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,
856 (GLfloat) light->Model.LocalViewer);
857 _mesa_LightModelf(GL_LIGHT_MODEL_TWO_SIDE,
858 (GLfloat) light->Model.TwoSide);
859 _mesa_LightModelf(GL_LIGHT_MODEL_COLOR_CONTROL,
860 (GLfloat) light->Model.ColorControl);
861 /* shade model */
862 _mesa_ShadeModel(light->ShadeModel);
863 /* color material */
864 _mesa_ColorMaterial(light->ColorMaterialFace,
865 light->ColorMaterialMode);
866 _mesa_set_enable(ctx, GL_COLOR_MATERIAL,
867 light->ColorMaterialEnabled);
868 /* materials */
869 memcpy(&ctx->Light.Material, &light->Material,
870 sizeof(struct gl_material));
871 }
872 break;
873 case GL_LINE_BIT:
874 {
875 const struct gl_line_attrib *line;
876 line = (const struct gl_line_attrib *) attr->data;
877 _mesa_set_enable(ctx, GL_LINE_SMOOTH, line->SmoothFlag);
878 _mesa_set_enable(ctx, GL_LINE_STIPPLE, line->StippleFlag);
879 _mesa_LineStipple(line->StippleFactor, line->StipplePattern);
880 _mesa_LineWidth(line->Width);
881 }
882 break;
883 case GL_LIST_BIT:
884 memcpy( &ctx->List, attr->data, sizeof(struct gl_list_attrib) );
885 break;
886 case GL_PIXEL_MODE_BIT:
887 memcpy( &ctx->Pixel, attr->data, sizeof(struct gl_pixel_attrib) );
888 /* XXX what other pixel state needs to be set by function calls? */
889 _mesa_ReadBuffer(ctx->Pixel.ReadBuffer);
890 ctx->NewState |= _NEW_PIXEL;
891 break;
892 case GL_POINT_BIT:
893 {
894 const struct gl_point_attrib *point;
895 point = (const struct gl_point_attrib *) attr->data;
896 _mesa_PointSize(point->Size);
897 _mesa_set_enable(ctx, GL_POINT_SMOOTH, point->SmoothFlag);
898 }
899 break;
900 case GL_POLYGON_BIT:
901 {
902 const struct gl_polygon_attrib *polygon;
903 polygon = (const struct gl_polygon_attrib *) attr->data;
904 _mesa_CullFace(polygon->CullFaceMode);
905 _mesa_FrontFace(polygon->FrontFace);
906 _mesa_PolygonMode(GL_FRONT, polygon->FrontMode);
907 _mesa_PolygonMode(GL_BACK, polygon->BackMode);
908 _mesa_PolygonOffset(polygon->OffsetFactor,
909 polygon->OffsetUnits);
910 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, polygon->SmoothFlag);
911 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, polygon->StippleFlag);
912 _mesa_set_enable(ctx, GL_CULL_FACE, polygon->CullFlag);
913 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_POINT,
914 polygon->OffsetPoint);
915 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_LINE,
916 polygon->OffsetLine);
917 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL,
918 polygon->OffsetFill);
919 }
920 break;
921 case GL_POLYGON_STIPPLE_BIT:
922 memcpy( ctx->PolygonStipple, attr->data, 32*sizeof(GLuint) );
923 ctx->NewState |= _NEW_POLYGONSTIPPLE;
924 if (ctx->Driver.PolygonStipple)
925 ctx->Driver.PolygonStipple( ctx, (const GLubyte *) attr->data );
926 break;
927 case GL_SCISSOR_BIT:
928 {
929 const struct gl_scissor_attrib *scissor;
930 scissor = (const struct gl_scissor_attrib *) attr->data;
931 _mesa_Scissor(scissor->X, scissor->Y,
932 scissor->Width, scissor->Height);
933 _mesa_set_enable(ctx, GL_SCISSOR_TEST, scissor->Enabled);
934 }
935 break;
936 case GL_STENCIL_BUFFER_BIT:
937 {
938 const struct gl_stencil_attrib *stencil;
939 stencil = (const struct gl_stencil_attrib *) attr->data;
940 _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
941 _mesa_ClearStencil(stencil->Clear);
942 _mesa_StencilFunc(stencil->Function,
943 stencil->Ref,
944 stencil->ValueMask);
945 _mesa_StencilMask(stencil->WriteMask);
946 _mesa_StencilOp(stencil->FailFunc,
947 stencil->ZFailFunc,
948 stencil->ZPassFunc);
949 }
950 break;
951 case GL_TRANSFORM_BIT:
952 {
953 GLuint i;
954 const struct gl_transform_attrib *xform;
955 xform = (const struct gl_transform_attrib *) attr->data;
956 _mesa_MatrixMode(xform->MatrixMode);
957 if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
958 _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
959
960 /* restore clip planes */
961 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
962 const GLuint mask = 1 << i;
963 const GLfloat *eyePlane = xform->EyeUserPlane[i];
964 COPY_4V(ctx->Transform.EyeUserPlane[i], eyePlane);
965 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i,
966 !!(xform->ClipPlanesEnabled & mask));
967 if (ctx->Driver.ClipPlane)
968 ctx->Driver.ClipPlane( ctx, GL_CLIP_PLANE0 + i, eyePlane );
969 }
970
971 /* normalize/rescale */
972 if (xform->Normalize != ctx->Transform.Normalize)
973 _mesa_set_enable(ctx, GL_NORMALIZE,ctx->Transform.Normalize);
974 if (xform->RescaleNormals != ctx->Transform.RescaleNormals)
975 _mesa_set_enable(ctx, GL_RESCALE_NORMAL_EXT,
976 ctx->Transform.RescaleNormals);
977 }
978 break;
979 case GL_TEXTURE_BIT:
980 /* Take care of texture object reference counters */
981 {
982 struct texture_state *texstate
983 = (struct texture_state *) attr->data;
984 pop_texture_group(ctx, texstate);
985 ctx->NewState |= _NEW_TEXTURE;
986 }
987 break;
988 case GL_VIEWPORT_BIT:
989 {
990 const struct gl_viewport_attrib *vp;
991 vp = (const struct gl_viewport_attrib *) attr->data;
992 _mesa_Viewport(vp->X, vp->Y, vp->Width, vp->Height);
993 _mesa_DepthRange(vp->Near, vp->Far);
994 }
995 break;
996 case GL_MULTISAMPLE_BIT_ARB:
997 {
998 const struct gl_multisample_attrib *ms;
999 ms = (const struct gl_multisample_attrib *) attr->data;
1000
1001 TEST_AND_UPDATE(ctx->Multisample.Enabled,
1002 ms->Enabled,
1003 GL_MULTISAMPLE);
1004
1005 TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
1006 ms->SampleCoverage,
1007 GL_SAMPLE_COVERAGE);
1008
1009 TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
1010 ms->SampleAlphaToCoverage,
1011 GL_SAMPLE_ALPHA_TO_COVERAGE);
1012
1013 TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
1014 ms->SampleAlphaToOne,
1015 GL_SAMPLE_ALPHA_TO_ONE);
1016
1017 _mesa_SampleCoverageARB(ms->SampleCoverageValue,
1018 ms->SampleCoverageInvert);
1019 }
1020 break;
1021
1022 default:
1023 _mesa_problem( ctx, "Bad attrib flag in PopAttrib");
1024 break;
1025 }
1026
1027 next = attr->next;
1028 FREE( attr->data );
1029 FREE( attr );
1030 attr = next;
1031 }
1032 }
1033
1034
1035 /**
1036 * Copy gl_pixelstore_attrib from src to dst, updating buffer
1037 * object refcounts.
1038 */
1039 static void
1040 copy_pixelstore(struct gl_context *ctx,
1041 struct gl_pixelstore_attrib *dst,
1042 const struct gl_pixelstore_attrib *src)
1043 {
1044 dst->Alignment = src->Alignment;
1045 dst->RowLength = src->RowLength;
1046 dst->SkipPixels = src->SkipPixels;
1047 dst->SkipRows = src->SkipRows;
1048 dst->ImageHeight = src->ImageHeight;
1049 dst->SkipImages = src->SkipImages;
1050 dst->SwapBytes = src->SwapBytes;
1051 dst->LsbFirst = src->LsbFirst;
1052 dst->Invert = src->Invert;
1053 }
1054
1055
1056 #define GL_CLIENT_PACK_BIT (1<<20)
1057 #define GL_CLIENT_UNPACK_BIT (1<<21)
1058
1059 /**
1060 * Copy gl_array_attrib from src to dest.
1061 * 'dest' must be in an initialized state.
1062 */
1063 static void
1064 copy_array_attrib(struct gl_context *ctx,
1065 struct gl_array_attrib *dest,
1066 struct gl_array_attrib *src,
1067 GLboolean vbo_deleted)
1068 {
1069 /* skip ArrayObj */
1070 /* skip DefaultArrayObj, Objects */
1071 dest->LockFirst = src->LockFirst;
1072 dest->LockCount = src->LockCount;
1073 /* skip NewState */
1074 /* skip RebindArrays */
1075
1076 if (!vbo_deleted)
1077 {
1078 GLuint i;
1079
1080 /* skip Name */
1081 /* skip RefCount */
1082
1083 for (i = 0; i < Elements(src->VertexAttrib); i++)
1084 _mesa_copy_client_array(ctx, &dest->VertexAttrib[i], &src->VertexAttrib[i]);
1085
1086 /* _Enabled must be the same than on push */
1087 dest->_Enabled = src->_Enabled;
1088 dest->_MaxElement = src->_MaxElement;
1089 }
1090
1091 /* skip ArrayBufferObj */
1092 /* skip ElementArrayBufferObj */
1093 }
1094
1095 /**
1096 * Save the content of src to dest.
1097 */
1098 static void
1099 save_array_attrib(struct gl_context *ctx,
1100 struct gl_array_attrib *dest,
1101 struct gl_array_attrib *src)
1102 {
1103 /* And copy all of the rest. */
1104 copy_array_attrib(ctx, dest, src, GL_FALSE);
1105
1106 /* Just reference them here */
1107 _mesa_reference_buffer_object(ctx, &dest->ArrayBufferObj,
1108 src->ArrayBufferObj);
1109 _mesa_reference_buffer_object(ctx, &dest->ElementArrayBufferObj,
1110 src->ElementArrayBufferObj);
1111 }
1112
1113 /**
1114 * Restore the content of src to dest.
1115 */
1116 static void
1117 restore_array_attrib(struct gl_context *ctx,
1118 struct gl_array_attrib *dest,
1119 struct gl_array_attrib *src)
1120 {
1121 /* Restore or recreate the buffer objects by the names ... */
1122 if (!src->ArrayBufferObj->Name == 0
1123 || _mesa_IsBufferARB(src->ArrayBufferObj->Name)) {
1124 /* ... and restore its content */
1125 copy_array_attrib(ctx, dest, src, GL_FALSE);
1126
1127 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB,
1128 src->ArrayBufferObj->Name);
1129 } else {
1130 copy_array_attrib(ctx, dest, src, GL_TRUE);
1131 }
1132
1133 if (src->ElementArrayBufferObj->Name == 0
1134 || _mesa_IsBufferARB(src->ElementArrayBufferObj->Name))
1135 _mesa_BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
1136 src->ElementArrayBufferObj->Name);
1137
1138 /* Better safe than sorry?! */
1139 dest->RebindArrays = GL_TRUE;
1140
1141 /* FIXME: Should some bits in ctx->Array->NewState also be set
1142 * FIXME: here? It seems like it should be set to inclusive-or
1143 * FIXME: of the old ArrayObj->_Enabled and the new _Enabled.
1144 * ... just do it.
1145 */
1146 dest->NewState |= src->_Enabled | dest->_Enabled;
1147 }
1148
1149 /**
1150 * Free/unreference the fields of 'attrib' but don't delete it (that's
1151 * done later in the calling code).
1152 * Needs to the cleanup part matching init_array_attrib_data above.
1153 */
1154 static void
1155 free_array_attrib_data(struct gl_context *ctx,
1156 struct gl_array_attrib *attrib)
1157 {
1158 _mesa_reference_buffer_object(ctx, &attrib->ArrayBufferObj, NULL);
1159 _mesa_free_varray_data(ctx, attrib);
1160 }
1161
1162
1163 void GLAPIENTRY
1164 _mesa_PushClientAttrib(GLbitfield mask)
1165 {
1166 struct gl_attrib_node *head;
1167
1168 GET_CURRENT_CONTEXT(ctx);
1169 ASSERT_OUTSIDE_BEGIN_END(ctx);
1170
1171 if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
1172 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushClientAttrib" );
1173 return;
1174 }
1175
1176 /* Build linked list of attribute nodes which save all attribute
1177 * groups specified by the mask.
1178 */
1179 head = NULL;
1180
1181 if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
1182 struct gl_pixelstore_attrib *attr;
1183 /* packing attribs */
1184 attr = CALLOC_STRUCT( gl_pixelstore_attrib );
1185 copy_pixelstore(ctx, attr, &ctx->Pack);
1186 save_attrib_data(&head, GL_CLIENT_PACK_BIT, attr);
1187 /* unpacking attribs */
1188 attr = CALLOC_STRUCT( gl_pixelstore_attrib );
1189 copy_pixelstore(ctx, attr, &ctx->Unpack);
1190 save_attrib_data(&head, GL_CLIENT_UNPACK_BIT, attr);
1191 }
1192
1193 if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1194 struct gl_array_attrib *attr;
1195 attr = CALLOC_STRUCT( gl_array_attrib );
1196 _mesa_init_varray(ctx, attr);
1197 save_array_attrib(ctx, attr, &ctx->Array);
1198 save_attrib_data(&head, GL_CLIENT_VERTEX_ARRAY_BIT, attr);
1199 }
1200
1201 ctx->ClientAttribStack[ctx->ClientAttribStackDepth] = head;
1202 ctx->ClientAttribStackDepth++;
1203 }
1204
1205
1206
1207
1208 void GLAPIENTRY
1209 _mesa_PopClientAttrib(void)
1210 {
1211 struct gl_attrib_node *node, *next;
1212
1213 GET_CURRENT_CONTEXT(ctx);
1214 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1215
1216 if (ctx->ClientAttribStackDepth == 0) {
1217 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib" );
1218 return;
1219 }
1220
1221 ctx->ClientAttribStackDepth--;
1222 node = ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
1223
1224 while (node) {
1225 switch (node->kind) {
1226 case GL_CLIENT_PACK_BIT:
1227 {
1228 struct gl_pixelstore_attrib *store =
1229 (struct gl_pixelstore_attrib *) node->data;
1230 copy_pixelstore(ctx, &ctx->Pack, store);
1231 }
1232 ctx->NewState |= _NEW_PACKUNPACK;
1233 break;
1234 case GL_CLIENT_UNPACK_BIT:
1235 {
1236 struct gl_pixelstore_attrib *store =
1237 (struct gl_pixelstore_attrib *) node->data;
1238 copy_pixelstore(ctx, &ctx->Unpack, store);
1239 }
1240 ctx->NewState |= _NEW_PACKUNPACK;
1241 break;
1242 case GL_CLIENT_VERTEX_ARRAY_BIT: {
1243 struct gl_array_attrib * attr =
1244 (struct gl_array_attrib *) node->data;
1245 restore_array_attrib(ctx, &ctx->Array, attr);
1246 free_array_attrib_data(ctx, attr);
1247 ctx->NewState |= _NEW_ARRAY;
1248 break;
1249 }
1250 default:
1251 _mesa_problem( ctx, "Bad attrib flag in PopClientAttrib");
1252 break;
1253 }
1254
1255 next = node->next;
1256 FREE( node->data );
1257 FREE( node );
1258 node = next;
1259 }
1260 }
1261
1262
1263 void
1264 _mesa_init_attrib_dispatch(struct _glapi_table *disp)
1265 {
1266 SET_PopAttrib(disp, _mesa_PopAttrib);
1267 SET_PushAttrib(disp, _mesa_PushAttrib);
1268 SET_PopClientAttrib(disp, _mesa_PopClientAttrib);
1269 SET_PushClientAttrib(disp, _mesa_PushClientAttrib);
1270 }
1271
1272
1273 #endif /* FEATURE_attrib_stack */
1274
1275
1276 /**
1277 * Free any attribute state data that might be attached to the context.
1278 */
1279 void
1280 _mesa_free_attrib_data(struct gl_context *ctx)
1281 {
1282 while (ctx->AttribStackDepth > 0) {
1283 struct gl_attrib_node *attr, *next;
1284
1285 ctx->AttribStackDepth--;
1286 attr = ctx->AttribStack[ctx->AttribStackDepth];
1287
1288 while (attr) {
1289 if (attr->kind == GL_TEXTURE_BIT) {
1290 struct texture_state *texstate = (struct texture_state*)attr->data;
1291 GLuint tgt;
1292 /* clear references to the saved texture objects */
1293 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
1294 _mesa_reference_texobj(&texstate->SavedTexRef[tgt], NULL);
1295 }
1296 _mesa_reference_shared_state(ctx, &texstate->SharedRef, NULL);
1297 }
1298 else {
1299 /* any other chunks of state that requires special handling? */
1300 }
1301
1302 next = attr->next;
1303 free(attr->data);
1304 free(attr);
1305 attr = next;
1306 }
1307 }
1308 }
1309
1310
1311 void _mesa_init_attrib( struct gl_context *ctx )
1312 {
1313 /* Renderer and client attribute stacks */
1314 ctx->AttribStackDepth = 0;
1315 ctx->ClientAttribStackDepth = 0;
1316 }