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