attrib.c revision 049e320f46f3a3daaa36ef67cc680dc504c124d5
1/*
2 * Mesa 3-D graphics library
3 * Version:  6.3
4 *
5 * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#include "glheader.h"
26#include "imports.h"
27#include "accum.h"
28#include "attrib.h"
29#include "blend.h"
30#include "buffers.h"
31#include "bufferobj.h"
32#include "colormac.h"
33#include "colortab.h"
34#include "context.h"
35#include "depth.h"
36#include "enable.h"
37#include "enums.h"
38#include "fog.h"
39#include "hint.h"
40#include "light.h"
41#include "lines.h"
42#include "matrix.h"
43#include "points.h"
44#include "polygon.h"
45#include "simple_list.h"
46#include "stencil.h"
47#include "texobj.h"
48#include "texstate.h"
49#include "mtypes.h"
50#include "math/m_xform.h"
51
52
53/*
54 * Allocate a new attribute state node.  These nodes have a
55 * "kind" value and a pointer to a struct of state data.
56 */
57static struct gl_attrib_node *
58new_attrib_node( GLbitfield kind )
59{
60   struct gl_attrib_node *an = MALLOC_STRUCT(gl_attrib_node);
61   if (an) {
62      an->kind = kind;
63   }
64   return an;
65}
66
67
68void GLAPIENTRY
69_mesa_PushAttrib(GLbitfield mask)
70{
71   struct gl_attrib_node *newnode;
72   struct gl_attrib_node *head;
73
74   GET_CURRENT_CONTEXT(ctx);
75   ASSERT_OUTSIDE_BEGIN_END(ctx);
76
77   if (MESA_VERBOSE & VERBOSE_API)
78      _mesa_debug(ctx, "glPushAttrib %x\n", (int) mask);
79
80   if (ctx->AttribStackDepth >= MAX_ATTRIB_STACK_DEPTH) {
81      _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushAttrib" );
82      return;
83   }
84
85   /* Build linked list of attribute nodes which save all attribute */
86   /* groups specified by the mask. */
87   head = NULL;
88
89   if (mask & GL_ACCUM_BUFFER_BIT) {
90      struct gl_accum_attrib *attr;
91      attr = MALLOC_STRUCT( gl_accum_attrib );
92      MEMCPY( attr, &ctx->Accum, sizeof(struct gl_accum_attrib) );
93      newnode = new_attrib_node( GL_ACCUM_BUFFER_BIT );
94      newnode->data = attr;
95      newnode->next = head;
96      head = newnode;
97   }
98
99   if (mask & GL_COLOR_BUFFER_BIT) {
100      struct gl_colorbuffer_attrib *attr;
101      attr = MALLOC_STRUCT( gl_colorbuffer_attrib );
102      MEMCPY( attr, &ctx->Color, sizeof(struct gl_colorbuffer_attrib) );
103      newnode = new_attrib_node( GL_COLOR_BUFFER_BIT );
104      newnode->data = attr;
105      newnode->next = head;
106      head = newnode;
107   }
108
109   if (mask & GL_CURRENT_BIT) {
110      struct gl_current_attrib *attr;
111      FLUSH_CURRENT( ctx, 0 );
112      attr = MALLOC_STRUCT( gl_current_attrib );
113      MEMCPY( attr, &ctx->Current, sizeof(struct gl_current_attrib) );
114      newnode = new_attrib_node( GL_CURRENT_BIT );
115      newnode->data = attr;
116      newnode->next = head;
117      head = newnode;
118   }
119
120   if (mask & GL_DEPTH_BUFFER_BIT) {
121      struct gl_depthbuffer_attrib *attr;
122      attr = MALLOC_STRUCT( gl_depthbuffer_attrib );
123      MEMCPY( attr, &ctx->Depth, sizeof(struct gl_depthbuffer_attrib) );
124      newnode = new_attrib_node( GL_DEPTH_BUFFER_BIT );
125      newnode->data = attr;
126      newnode->next = head;
127      head = newnode;
128   }
129
130   if (mask & GL_ENABLE_BIT) {
131      struct gl_enable_attrib *attr;
132      GLuint i;
133      attr = MALLOC_STRUCT( gl_enable_attrib );
134      /* Copy enable flags from all other attributes into the enable struct. */
135      attr->AlphaTest = ctx->Color.AlphaEnabled;
136      attr->AutoNormal = ctx->Eval.AutoNormal;
137      attr->Blend = ctx->Color.BlendEnabled;
138      attr->ClipPlanes = ctx->Transform.ClipPlanesEnabled;
139      attr->ColorMaterial = ctx->Light.ColorMaterialEnabled;
140      attr->ColorTable = ctx->Pixel.ColorTableEnabled;
141      attr->PostColorMatrixColorTable = ctx->Pixel.PostColorMatrixColorTableEnabled;
142      attr->PostConvolutionColorTable = ctx->Pixel.PostConvolutionColorTableEnabled;
143      attr->Convolution1D = ctx->Pixel.Convolution1DEnabled;
144      attr->Convolution2D = ctx->Pixel.Convolution2DEnabled;
145      attr->Separable2D = ctx->Pixel.Separable2DEnabled;
146      attr->CullFace = ctx->Polygon.CullFlag;
147      attr->DepthTest = ctx->Depth.Test;
148      attr->Dither = ctx->Color.DitherFlag;
149      attr->Fog = ctx->Fog.Enabled;
150      for (i=0;i<MAX_LIGHTS;i++) {
151         attr->Light[i] = ctx->Light.Light[i].Enabled;
152      }
153      attr->Lighting = ctx->Light.Enabled;
154      attr->LineSmooth = ctx->Line.SmoothFlag;
155      attr->LineStipple = ctx->Line.StippleFlag;
156      attr->Histogram = ctx->Pixel.HistogramEnabled;
157      attr->MinMax = ctx->Pixel.MinMaxEnabled;
158      attr->IndexLogicOp = ctx->Color.IndexLogicOpEnabled;
159      attr->ColorLogicOp = ctx->Color.ColorLogicOpEnabled;
160      attr->Map1Color4 = ctx->Eval.Map1Color4;
161      attr->Map1Index = ctx->Eval.Map1Index;
162      attr->Map1Normal = ctx->Eval.Map1Normal;
163      attr->Map1TextureCoord1 = ctx->Eval.Map1TextureCoord1;
164      attr->Map1TextureCoord2 = ctx->Eval.Map1TextureCoord2;
165      attr->Map1TextureCoord3 = ctx->Eval.Map1TextureCoord3;
166      attr->Map1TextureCoord4 = ctx->Eval.Map1TextureCoord4;
167      attr->Map1Vertex3 = ctx->Eval.Map1Vertex3;
168      attr->Map1Vertex4 = ctx->Eval.Map1Vertex4;
169      MEMCPY(attr->Map1Attrib, ctx->Eval.Map1Attrib, sizeof(ctx->Eval.Map1Attrib));
170      attr->Map2Color4 = ctx->Eval.Map2Color4;
171      attr->Map2Index = ctx->Eval.Map2Index;
172      attr->Map2Normal = ctx->Eval.Map2Normal;
173      attr->Map2TextureCoord1 = ctx->Eval.Map2TextureCoord1;
174      attr->Map2TextureCoord2 = ctx->Eval.Map2TextureCoord2;
175      attr->Map2TextureCoord3 = ctx->Eval.Map2TextureCoord3;
176      attr->Map2TextureCoord4 = ctx->Eval.Map2TextureCoord4;
177      attr->Map2Vertex3 = ctx->Eval.Map2Vertex3;
178      attr->Map2Vertex4 = ctx->Eval.Map2Vertex4;
179      MEMCPY(attr->Map2Attrib, ctx->Eval.Map2Attrib, sizeof(ctx->Eval.Map2Attrib));
180      attr->Normalize = ctx->Transform.Normalize;
181      attr->RasterPositionUnclipped = ctx->Transform.RasterPositionUnclipped;
182      attr->PixelTexture = ctx->Pixel.PixelTextureEnabled;
183      attr->PointSmooth = ctx->Point.SmoothFlag;
184      attr->PointSprite = ctx->Point.PointSprite;
185      attr->PolygonOffsetPoint = ctx->Polygon.OffsetPoint;
186      attr->PolygonOffsetLine = ctx->Polygon.OffsetLine;
187      attr->PolygonOffsetFill = ctx->Polygon.OffsetFill;
188      attr->PolygonSmooth = ctx->Polygon.SmoothFlag;
189      attr->PolygonStipple = ctx->Polygon.StippleFlag;
190      attr->RescaleNormals = ctx->Transform.RescaleNormals;
191      attr->Scissor = ctx->Scissor.Enabled;
192      attr->Stencil = ctx->Stencil.Enabled;
193      attr->StencilTwoSide = ctx->Stencil.TestTwoSide;
194      attr->MultisampleEnabled = ctx->Multisample.Enabled;
195      attr->SampleAlphaToCoverage = ctx->Multisample.SampleAlphaToCoverage;
196      attr->SampleAlphaToOne = ctx->Multisample.SampleAlphaToOne;
197      attr->SampleCoverage = ctx->Multisample.SampleCoverage;
198      attr->SampleCoverageInvert = ctx->Multisample.SampleCoverageInvert;
199      for (i=0; i<MAX_TEXTURE_UNITS; i++) {
200         attr->Texture[i] = ctx->Texture.Unit[i].Enabled;
201         attr->TexGen[i] = ctx->Texture.Unit[i].TexGenEnabled;
202         attr->TextureColorTable[i] = ctx->Texture.Unit[i].ColorTableEnabled;
203      }
204      /* GL_NV_vertex_program */
205      attr->VertexProgram = ctx->VertexProgram.Enabled;
206      attr->VertexProgramPointSize = ctx->VertexProgram.PointSizeEnabled;
207      attr->VertexProgramTwoSide = ctx->VertexProgram.TwoSideEnabled;
208      newnode = new_attrib_node( GL_ENABLE_BIT );
209      newnode->data = attr;
210      newnode->next = head;
211      head = newnode;
212   }
213
214   if (mask & GL_EVAL_BIT) {
215      struct gl_eval_attrib *attr;
216      attr = MALLOC_STRUCT( gl_eval_attrib );
217      MEMCPY( attr, &ctx->Eval, sizeof(struct gl_eval_attrib) );
218      newnode = new_attrib_node( GL_EVAL_BIT );
219      newnode->data = attr;
220      newnode->next = head;
221      head = newnode;
222   }
223
224   if (mask & GL_FOG_BIT) {
225      struct gl_fog_attrib *attr;
226      attr = MALLOC_STRUCT( gl_fog_attrib );
227      MEMCPY( attr, &ctx->Fog, sizeof(struct gl_fog_attrib) );
228      newnode = new_attrib_node( GL_FOG_BIT );
229      newnode->data = attr;
230      newnode->next = head;
231      head = newnode;
232   }
233
234   if (mask & GL_HINT_BIT) {
235      struct gl_hint_attrib *attr;
236      attr = MALLOC_STRUCT( gl_hint_attrib );
237      MEMCPY( attr, &ctx->Hint, sizeof(struct gl_hint_attrib) );
238      newnode = new_attrib_node( GL_HINT_BIT );
239      newnode->data = attr;
240      newnode->next = head;
241      head = newnode;
242   }
243
244   if (mask & GL_LIGHTING_BIT) {
245      struct gl_light_attrib *attr;
246      FLUSH_CURRENT(ctx, 0);	/* flush material changes */
247      attr = MALLOC_STRUCT( gl_light_attrib );
248      MEMCPY( attr, &ctx->Light, sizeof(struct gl_light_attrib) );
249      newnode = new_attrib_node( GL_LIGHTING_BIT );
250      newnode->data = attr;
251      newnode->next = head;
252      head = newnode;
253   }
254
255   if (mask & GL_LINE_BIT) {
256      struct gl_line_attrib *attr;
257      attr = MALLOC_STRUCT( gl_line_attrib );
258      MEMCPY( attr, &ctx->Line, sizeof(struct gl_line_attrib) );
259      newnode = new_attrib_node( GL_LINE_BIT );
260      newnode->data = attr;
261      newnode->next = head;
262      head = newnode;
263   }
264
265   if (mask & GL_LIST_BIT) {
266      struct gl_list_attrib *attr;
267      attr = MALLOC_STRUCT( gl_list_attrib );
268      MEMCPY( attr, &ctx->List, sizeof(struct gl_list_attrib) );
269      newnode = new_attrib_node( GL_LIST_BIT );
270      newnode->data = attr;
271      newnode->next = head;
272      head = newnode;
273   }
274
275   if (mask & GL_PIXEL_MODE_BIT) {
276      struct gl_pixel_attrib *attr;
277      attr = MALLOC_STRUCT( gl_pixel_attrib );
278      MEMCPY( attr, &ctx->Pixel, sizeof(struct gl_pixel_attrib) );
279      newnode = new_attrib_node( GL_PIXEL_MODE_BIT );
280      newnode->data = attr;
281      newnode->next = head;
282      head = newnode;
283   }
284
285   if (mask & GL_POINT_BIT) {
286      struct gl_point_attrib *attr;
287      attr = MALLOC_STRUCT( gl_point_attrib );
288      MEMCPY( attr, &ctx->Point, sizeof(struct gl_point_attrib) );
289      newnode = new_attrib_node( GL_POINT_BIT );
290      newnode->data = attr;
291      newnode->next = head;
292      head = newnode;
293   }
294
295   if (mask & GL_POLYGON_BIT) {
296      struct gl_polygon_attrib *attr;
297      attr = MALLOC_STRUCT( gl_polygon_attrib );
298      MEMCPY( attr, &ctx->Polygon, sizeof(struct gl_polygon_attrib) );
299      newnode = new_attrib_node( GL_POLYGON_BIT );
300      newnode->data = attr;
301      newnode->next = head;
302      head = newnode;
303   }
304
305   if (mask & GL_POLYGON_STIPPLE_BIT) {
306      GLuint *stipple;
307      stipple = (GLuint *) MALLOC( 32*sizeof(GLuint) );
308      MEMCPY( stipple, ctx->PolygonStipple, 32*sizeof(GLuint) );
309      newnode = new_attrib_node( GL_POLYGON_STIPPLE_BIT );
310      newnode->data = stipple;
311      newnode->next = head;
312      head = newnode;
313   }
314
315   if (mask & GL_SCISSOR_BIT) {
316      struct gl_scissor_attrib *attr;
317      attr = MALLOC_STRUCT( gl_scissor_attrib );
318      MEMCPY( attr, &ctx->Scissor, sizeof(struct gl_scissor_attrib) );
319      newnode = new_attrib_node( GL_SCISSOR_BIT );
320      newnode->data = attr;
321      newnode->next = head;
322      head = newnode;
323   }
324
325   if (mask & GL_STENCIL_BUFFER_BIT) {
326      struct gl_stencil_attrib *attr;
327      attr = MALLOC_STRUCT( gl_stencil_attrib );
328      MEMCPY( attr, &ctx->Stencil, sizeof(struct gl_stencil_attrib) );
329      newnode = new_attrib_node( GL_STENCIL_BUFFER_BIT );
330      newnode->data = attr;
331      newnode->next = head;
332      head = newnode;
333   }
334
335   if (mask & GL_TEXTURE_BIT) {
336      struct gl_texture_attrib *attr;
337      GLuint u;
338      /* Bump the texture object reference counts so that they don't
339       * inadvertantly get deleted.
340       */
341      for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
342	 ctx->Texture.Unit[u].Current1D->RefCount++;
343	 ctx->Texture.Unit[u].Current2D->RefCount++;
344	 ctx->Texture.Unit[u].Current3D->RefCount++;
345	 ctx->Texture.Unit[u].CurrentCubeMap->RefCount++;
346	 ctx->Texture.Unit[u].CurrentRect->RefCount++;
347      }
348      attr = MALLOC_STRUCT( gl_texture_attrib );
349      MEMCPY( attr, &ctx->Texture, sizeof(struct gl_texture_attrib) );
350      /* copy state of the currently bound texture objects */
351      for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
352         _mesa_copy_texture_object(&attr->Unit[u].Saved1D,
353                                   attr->Unit[u].Current1D);
354         _mesa_copy_texture_object(&attr->Unit[u].Saved2D,
355                                   attr->Unit[u].Current2D);
356         _mesa_copy_texture_object(&attr->Unit[u].Saved3D,
357                                   attr->Unit[u].Current3D);
358         _mesa_copy_texture_object(&attr->Unit[u].SavedCubeMap,
359                                   attr->Unit[u].CurrentCubeMap);
360         _mesa_copy_texture_object(&attr->Unit[u].SavedRect,
361                                   attr->Unit[u].CurrentRect);
362      }
363      newnode = new_attrib_node( GL_TEXTURE_BIT );
364      newnode->data = attr;
365      newnode->next = head;
366      head = newnode;
367   }
368
369   if (mask & GL_TRANSFORM_BIT) {
370      struct gl_transform_attrib *attr;
371      attr = MALLOC_STRUCT( gl_transform_attrib );
372      MEMCPY( attr, &ctx->Transform, sizeof(struct gl_transform_attrib) );
373      newnode = new_attrib_node( GL_TRANSFORM_BIT );
374      newnode->data = attr;
375      newnode->next = head;
376      head = newnode;
377   }
378
379   if (mask & GL_VIEWPORT_BIT) {
380      struct gl_viewport_attrib *attr;
381      attr = MALLOC_STRUCT( gl_viewport_attrib );
382      MEMCPY( attr, &ctx->Viewport, sizeof(struct gl_viewport_attrib) );
383      newnode = new_attrib_node( GL_VIEWPORT_BIT );
384      newnode->data = attr;
385      newnode->next = head;
386      head = newnode;
387   }
388
389   /* GL_ARB_multisample */
390   if (mask & GL_MULTISAMPLE_BIT_ARB) {
391      struct gl_multisample_attrib *attr;
392      attr = MALLOC_STRUCT( gl_multisample_attrib );
393      MEMCPY( attr, &ctx->Multisample, sizeof(struct gl_multisample_attrib) );
394      newnode = new_attrib_node( GL_MULTISAMPLE_BIT_ARB );
395      newnode->data = attr;
396      newnode->next = head;
397      head = newnode;
398   }
399
400   ctx->AttribStack[ctx->AttribStackDepth] = head;
401   ctx->AttribStackDepth++;
402}
403
404
405
406static void
407pop_enable_group(GLcontext *ctx, const struct gl_enable_attrib *enable)
408{
409   GLuint i;
410
411#define TEST_AND_UPDATE(VALUE, NEWVALUE, ENUM)		\
412	if ((VALUE) != (NEWVALUE)) {			\
413	   _mesa_set_enable( ctx, ENUM, (NEWVALUE) );	\
414	}
415
416   TEST_AND_UPDATE(ctx->Color.AlphaEnabled, enable->AlphaTest, GL_ALPHA_TEST);
417   TEST_AND_UPDATE(ctx->Color.BlendEnabled, enable->Blend, GL_BLEND);
418
419   for (i=0;i<MAX_CLIP_PLANES;i++) {
420      const GLuint mask = 1 << i;
421      if ((ctx->Transform.ClipPlanesEnabled & mask) != (enable->ClipPlanes & mask))
422	  _mesa_set_enable(ctx, (GLenum) (GL_CLIP_PLANE0 + i),
423			   (GLboolean) ((enable->ClipPlanes & mask) ? GL_TRUE : GL_FALSE));
424   }
425
426   TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled, enable->ColorMaterial,
427                   GL_COLOR_MATERIAL);
428   TEST_AND_UPDATE(ctx->Pixel.ColorTableEnabled, enable->ColorTable,
429                   GL_COLOR_TABLE);
430   TEST_AND_UPDATE(ctx->Pixel.PostColorMatrixColorTableEnabled,
431                   enable->PostColorMatrixColorTable,
432                   GL_POST_COLOR_MATRIX_COLOR_TABLE);
433   TEST_AND_UPDATE(ctx->Pixel.PostConvolutionColorTableEnabled,
434                   enable->PostConvolutionColorTable,
435                   GL_POST_CONVOLUTION_COLOR_TABLE);
436   TEST_AND_UPDATE(ctx->Polygon.CullFlag, enable->CullFace, GL_CULL_FACE);
437   TEST_AND_UPDATE(ctx->Depth.Test, enable->DepthTest, GL_DEPTH_TEST);
438   TEST_AND_UPDATE(ctx->Color.DitherFlag, enable->Dither, GL_DITHER);
439   TEST_AND_UPDATE(ctx->Pixel.Convolution1DEnabled, enable->Convolution1D,
440                   GL_CONVOLUTION_1D);
441   TEST_AND_UPDATE(ctx->Pixel.Convolution2DEnabled, enable->Convolution2D,
442                   GL_CONVOLUTION_2D);
443   TEST_AND_UPDATE(ctx->Pixel.Separable2DEnabled, enable->Separable2D,
444                   GL_SEPARABLE_2D);
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   for (i = 0; i < 16; i++) {
471      TEST_AND_UPDATE(ctx->Eval.Map1Attrib[i], enable->Map1Attrib[i],
472                      GL_MAP1_VERTEX_ATTRIB0_4_NV + i);
473   }
474
475   TEST_AND_UPDATE(ctx->Eval.Map2Color4, enable->Map2Color4, GL_MAP2_COLOR_4);
476   TEST_AND_UPDATE(ctx->Eval.Map2Index, enable->Map2Index, GL_MAP2_INDEX);
477   TEST_AND_UPDATE(ctx->Eval.Map2Normal, enable->Map2Normal, GL_MAP2_NORMAL);
478   TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord1, enable->Map2TextureCoord1,
479                   GL_MAP2_TEXTURE_COORD_1);
480   TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord2, enable->Map2TextureCoord2,
481                   GL_MAP2_TEXTURE_COORD_2);
482   TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord3, enable->Map2TextureCoord3,
483                   GL_MAP2_TEXTURE_COORD_3);
484   TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord4, enable->Map2TextureCoord4,
485                   GL_MAP2_TEXTURE_COORD_4);
486   TEST_AND_UPDATE(ctx->Eval.Map2Vertex3, enable->Map2Vertex3,
487                   GL_MAP2_VERTEX_3);
488   TEST_AND_UPDATE(ctx->Eval.Map2Vertex4, enable->Map2Vertex4,
489                   GL_MAP2_VERTEX_4);
490   for (i = 0; i < 16; i++) {
491      TEST_AND_UPDATE(ctx->Eval.Map2Attrib[i], enable->Map2Attrib[i],
492                      GL_MAP2_VERTEX_ATTRIB0_4_NV + i);
493   }
494
495   TEST_AND_UPDATE(ctx->Eval.AutoNormal, enable->AutoNormal, GL_AUTO_NORMAL);
496   TEST_AND_UPDATE(ctx->Transform.Normalize, enable->Normalize, GL_NORMALIZE);
497   TEST_AND_UPDATE(ctx->Transform.RescaleNormals, enable->RescaleNormals,
498                   GL_RESCALE_NORMAL_EXT);
499   TEST_AND_UPDATE(ctx->Transform.RasterPositionUnclipped,
500                   enable->RasterPositionUnclipped,
501                   GL_RASTER_POSITION_UNCLIPPED_IBM);
502   TEST_AND_UPDATE(ctx->Pixel.PixelTextureEnabled, enable->PixelTexture,
503                   GL_POINT_SMOOTH);
504   TEST_AND_UPDATE(ctx->Point.SmoothFlag, enable->PointSmooth,
505                   GL_POINT_SMOOTH);
506   if (ctx->Extensions.NV_point_sprite || ctx->Extensions.ARB_point_sprite) {
507      TEST_AND_UPDATE(ctx->Point.PointSprite, enable->PointSprite,
508                      GL_POINT_SPRITE_NV);
509   }
510   TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, enable->PolygonOffsetPoint,
511                   GL_POLYGON_OFFSET_POINT);
512   TEST_AND_UPDATE(ctx->Polygon.OffsetLine, enable->PolygonOffsetLine,
513                   GL_POLYGON_OFFSET_LINE);
514   TEST_AND_UPDATE(ctx->Polygon.OffsetFill, enable->PolygonOffsetFill,
515                   GL_POLYGON_OFFSET_FILL);
516   TEST_AND_UPDATE(ctx->Polygon.SmoothFlag, enable->PolygonSmooth,
517                   GL_POLYGON_SMOOTH);
518   TEST_AND_UPDATE(ctx->Polygon.StippleFlag, enable->PolygonStipple,
519                   GL_POLYGON_STIPPLE);
520   TEST_AND_UPDATE(ctx->Scissor.Enabled, enable->Scissor, GL_SCISSOR_TEST);
521   TEST_AND_UPDATE(ctx->Stencil.Enabled, enable->Stencil, GL_STENCIL_TEST);
522   if (ctx->Extensions.EXT_stencil_two_side) {
523      TEST_AND_UPDATE(ctx->Stencil.TestTwoSide, enable->StencilTwoSide, GL_STENCIL_TEST_TWO_SIDE_EXT);
524   }
525   TEST_AND_UPDATE(ctx->Multisample.Enabled, enable->MultisampleEnabled,
526                   GL_MULTISAMPLE_ARB);
527   TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
528                   enable->SampleAlphaToCoverage,
529                   GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
530   TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
531                   enable->SampleAlphaToOne,
532                   GL_SAMPLE_ALPHA_TO_ONE_ARB);
533   TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
534                   enable->SampleCoverage,
535                   GL_SAMPLE_COVERAGE_ARB);
536   TEST_AND_UPDATE(ctx->Multisample.SampleCoverageInvert,
537                   enable->SampleCoverageInvert,
538                   GL_SAMPLE_COVERAGE_INVERT_ARB);
539   /* GL_NV_vertex_program */
540   TEST_AND_UPDATE(ctx->VertexProgram.Enabled,
541                   enable->VertexProgram,
542                   GL_VERTEX_PROGRAM_NV);
543   TEST_AND_UPDATE(ctx->VertexProgram.PointSizeEnabled,
544                   enable->VertexProgramPointSize,
545                   GL_VERTEX_PROGRAM_POINT_SIZE_NV);
546   TEST_AND_UPDATE(ctx->VertexProgram.TwoSideEnabled,
547                   enable->VertexProgramTwoSide,
548                   GL_VERTEX_PROGRAM_TWO_SIDE_NV);
549
550#undef TEST_AND_UPDATE
551
552   /* texture unit enables */
553   for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
554      if (ctx->Texture.Unit[i].Enabled != enable->Texture[i]) {
555         ctx->Texture.Unit[i].Enabled = enable->Texture[i];
556         if (ctx->Driver.Enable) {
557            if (ctx->Driver.ActiveTexture) {
558               (*ctx->Driver.ActiveTexture)(ctx, i);
559            }
560            (*ctx->Driver.Enable)( ctx, GL_TEXTURE_1D,
561                             (GLboolean) (enable->Texture[i] & TEXTURE_1D_BIT) );
562            (*ctx->Driver.Enable)( ctx, GL_TEXTURE_2D,
563                             (GLboolean) (enable->Texture[i] & TEXTURE_2D_BIT) );
564            (*ctx->Driver.Enable)( ctx, GL_TEXTURE_3D,
565                             (GLboolean) (enable->Texture[i] & TEXTURE_3D_BIT) );
566            if (ctx->Extensions.ARB_texture_cube_map)
567               (*ctx->Driver.Enable)( ctx, GL_TEXTURE_CUBE_MAP_ARB,
568                          (GLboolean) (enable->Texture[i] & TEXTURE_CUBE_BIT) );
569            if (ctx->Extensions.NV_texture_rectangle)
570               (*ctx->Driver.Enable)( ctx, GL_TEXTURE_RECTANGLE_NV,
571                          (GLboolean) (enable->Texture[i] & TEXTURE_RECT_BIT) );
572         }
573      }
574
575      if (ctx->Texture.Unit[i].TexGenEnabled != enable->TexGen[i]) {
576         ctx->Texture.Unit[i].TexGenEnabled = enable->TexGen[i];
577         if (ctx->Driver.Enable) {
578            if (ctx->Driver.ActiveTexture) {
579               (*ctx->Driver.ActiveTexture)(ctx, i);
580            }
581            if (enable->TexGen[i] & S_BIT)
582               (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_S, GL_TRUE);
583            else
584               (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_S, GL_FALSE);
585            if (enable->TexGen[i] & T_BIT)
586               (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_T, GL_TRUE);
587            else
588               (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_T, GL_FALSE);
589            if (enable->TexGen[i] & R_BIT)
590               (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_R, GL_TRUE);
591            else
592               (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_R, GL_FALSE);
593            if (enable->TexGen[i] & Q_BIT)
594               (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_Q, GL_TRUE);
595            else
596               (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_Q, GL_FALSE);
597         }
598      }
599
600      /* GL_SGI_texture_color_table */
601      ctx->Texture.Unit[i].ColorTableEnabled = enable->TextureColorTable[i];
602   }
603
604   if (ctx->Driver.ActiveTexture) {
605      (*ctx->Driver.ActiveTexture)(ctx, ctx->Texture.CurrentUnit);
606   }
607}
608
609
610static void
611pop_texture_group(GLcontext *ctx, const struct gl_texture_attrib *texAttrib)
612{
613   GLuint u;
614
615   for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
616      const struct gl_texture_unit *unit = &texAttrib->Unit[u];
617      GLuint i;
618
619      _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + u);
620      _mesa_set_enable(ctx, GL_TEXTURE_1D,
621              (GLboolean) (unit->Enabled & TEXTURE_1D_BIT ? GL_TRUE : GL_FALSE));
622      _mesa_set_enable(ctx, GL_TEXTURE_2D,
623              (GLboolean) (unit->Enabled & TEXTURE_2D_BIT ? GL_TRUE : GL_FALSE));
624      _mesa_set_enable(ctx, GL_TEXTURE_3D,
625              (GLboolean) (unit->Enabled & TEXTURE_3D_BIT ? GL_TRUE : GL_FALSE));
626      if (ctx->Extensions.ARB_texture_cube_map) {
627         _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP_ARB,
628             (GLboolean) (unit->Enabled & TEXTURE_CUBE_BIT ? GL_TRUE : GL_FALSE));
629      }
630      if (ctx->Extensions.NV_texture_rectangle) {
631         _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE_NV,
632             (GLboolean) (unit->Enabled & TEXTURE_RECT_BIT ? GL_TRUE : GL_FALSE));
633      }
634      if (ctx->Extensions.SGI_texture_color_table) {
635         _mesa_set_enable(ctx, GL_TEXTURE_COLOR_TABLE_SGI,
636                          unit->ColorTableEnabled);
637      }
638      _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->EnvMode);
639      _mesa_TexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, unit->EnvColor);
640      _mesa_TexGeni(GL_S, GL_TEXTURE_GEN_MODE, unit->GenModeS);
641      _mesa_TexGeni(GL_T, GL_TEXTURE_GEN_MODE, unit->GenModeT);
642      _mesa_TexGeni(GL_R, GL_TEXTURE_GEN_MODE, unit->GenModeR);
643      _mesa_TexGeni(GL_Q, GL_TEXTURE_GEN_MODE, unit->GenModeQ);
644      _mesa_TexGenfv(GL_S, GL_OBJECT_PLANE, unit->ObjectPlaneS);
645      _mesa_TexGenfv(GL_T, GL_OBJECT_PLANE, unit->ObjectPlaneT);
646      _mesa_TexGenfv(GL_R, GL_OBJECT_PLANE, unit->ObjectPlaneR);
647      _mesa_TexGenfv(GL_Q, GL_OBJECT_PLANE, unit->ObjectPlaneQ);
648      _mesa_TexGenfv(GL_S, GL_EYE_PLANE, unit->EyePlaneS);
649      _mesa_TexGenfv(GL_T, GL_EYE_PLANE, unit->EyePlaneT);
650      _mesa_TexGenfv(GL_R, GL_EYE_PLANE, unit->EyePlaneR);
651      _mesa_TexGenfv(GL_Q, GL_EYE_PLANE, unit->EyePlaneQ);
652      _mesa_set_enable(ctx, GL_TEXTURE_GEN_S,
653                       ((unit->TexGenEnabled & S_BIT) ? GL_TRUE : GL_FALSE));
654      _mesa_set_enable(ctx, GL_TEXTURE_GEN_T,
655                       ((unit->TexGenEnabled & T_BIT) ? GL_TRUE : GL_FALSE));
656      _mesa_set_enable(ctx, GL_TEXTURE_GEN_R,
657                       ((unit->TexGenEnabled & R_BIT) ? GL_TRUE : GL_FALSE));
658      _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q,
659                       ((unit->TexGenEnabled & Q_BIT) ? GL_TRUE : GL_FALSE));
660      if (ctx->Extensions.EXT_texture_lod_bias) {
661         _mesa_TexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
662                       GL_TEXTURE_LOD_BIAS_EXT, unit->LodBias);
663      }
664      if (ctx->Extensions.EXT_texture_env_combine ||
665          ctx->Extensions.ARB_texture_env_combine) {
666         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,
667                       unit->Combine.ModeRGB);
668         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,
669                       unit->Combine.ModeA);
670         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB,
671                       unit->Combine.SourceRGB[0]);
672         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB,
673                       unit->Combine.SourceRGB[1]);
674         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB,
675                       unit->Combine.SourceRGB[2]);
676         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA,
677                       unit->Combine.SourceA[0]);
678         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA,
679                       unit->Combine.SourceA[1]);
680         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA,
681                       unit->Combine.SourceA[2]);
682         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB,
683                       unit->Combine.OperandRGB[0]);
684         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB,
685                       unit->Combine.OperandRGB[1]);
686         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB,
687                       unit->Combine.OperandRGB[2]);
688         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA,
689                       unit->Combine.OperandA[0]);
690         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA,
691                       unit->Combine.OperandA[1]);
692         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA,
693                       unit->Combine.OperandA[2]);
694         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE,
695                       1 << unit->Combine.ScaleShiftRGB);
696         _mesa_TexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE,
697                       1 << unit->Combine.ScaleShiftA);
698      }
699
700      /* Restore texture object state */
701      for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
702         GLenum target = 0;
703         const struct gl_texture_object *obj = NULL;
704         GLfloat bordColor[4];
705
706         switch (i) {
707         case 0:
708            target = GL_TEXTURE_1D;
709            obj = &unit->Saved1D;
710            break;
711         case 1:
712            target = GL_TEXTURE_2D;
713            obj = &unit->Saved2D;
714            break;
715         case 2:
716            target = GL_TEXTURE_3D;
717            obj = &unit->Saved3D;
718            break;
719         case 3:
720            if (!ctx->Extensions.ARB_texture_cube_map)
721               continue;
722            target = GL_TEXTURE_CUBE_MAP_ARB;
723            obj = &unit->SavedCubeMap;
724            break;
725         case 4:
726            if (!ctx->Extensions.NV_texture_rectangle)
727               continue;
728            target = GL_TEXTURE_RECTANGLE_NV;
729            obj = &unit->SavedRect;
730            break;
731         default:
732            ; /* silence warnings */
733         }
734
735         _mesa_BindTexture(target, obj->Name);
736
737         bordColor[0] = CHAN_TO_FLOAT(obj->BorderColor[0]);
738         bordColor[1] = CHAN_TO_FLOAT(obj->BorderColor[1]);
739         bordColor[2] = CHAN_TO_FLOAT(obj->BorderColor[2]);
740         bordColor[3] = CHAN_TO_FLOAT(obj->BorderColor[3]);
741
742         _mesa_TexParameterf(target, GL_TEXTURE_PRIORITY, obj->Priority);
743         _mesa_TexParameterfv(target, GL_TEXTURE_BORDER_COLOR, bordColor);
744         _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, obj->WrapS);
745         _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, obj->WrapT);
746         _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, obj->WrapR);
747         _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, obj->MinFilter);
748         _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, obj->MagFilter);
749         _mesa_TexParameterf(target, GL_TEXTURE_MIN_LOD, obj->MinLod);
750         _mesa_TexParameterf(target, GL_TEXTURE_MAX_LOD, obj->MaxLod);
751         _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, obj->BaseLevel);
752         _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, obj->MaxLevel);
753         if (ctx->Extensions.EXT_texture_filter_anisotropic) {
754            _mesa_TexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
755                                obj->MaxAnisotropy);
756         }
757         if (ctx->Extensions.SGIX_shadow) {
758            _mesa_TexParameteri(target, GL_TEXTURE_COMPARE_SGIX,
759                                obj->CompareFlag);
760            _mesa_TexParameteri(target, GL_TEXTURE_COMPARE_OPERATOR_SGIX,
761                                obj->CompareOperator);
762         }
763         if (ctx->Extensions.SGIX_shadow_ambient) {
764            _mesa_TexParameterf(target, GL_SHADOW_AMBIENT_SGIX,
765                                obj->ShadowAmbient);
766         }
767
768      }
769   }
770   _mesa_ActiveTextureARB(GL_TEXTURE0_ARB
771                          + texAttrib->CurrentUnit);
772
773   /* "un-bump" the texture object reference counts.  We did that so they
774    * wouldn't inadvertantly get deleted while they were still referenced
775    * inside the attribute state stack.
776    */
777   for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
778      ctx->Texture.Unit[u].Current1D->RefCount--;
779      ctx->Texture.Unit[u].Current2D->RefCount--;
780      ctx->Texture.Unit[u].Current3D->RefCount--;
781      ctx->Texture.Unit[u].CurrentCubeMap->RefCount--;
782      ctx->Texture.Unit[u].CurrentRect->RefCount--;
783   }
784}
785
786
787/*
788 * This function is kind of long just because we have to call a lot
789 * of device driver functions to update device driver state.
790 *
791 * XXX As it is now, most of the pop-code calls immediate-mode Mesa functions
792 * in order to restore GL state.  This isn't terribly efficient but it
793 * ensures that dirty flags and any derived state gets updated correctly.
794 * We could at least check if the value to restore equals the current value
795 * and then skip the Mesa call.
796 */
797void GLAPIENTRY
798_mesa_PopAttrib(void)
799{
800   struct gl_attrib_node *attr, *next;
801   GET_CURRENT_CONTEXT(ctx);
802   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
803
804   if (ctx->AttribStackDepth == 0) {
805      _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopAttrib" );
806      return;
807   }
808
809   ctx->AttribStackDepth--;
810   attr = ctx->AttribStack[ctx->AttribStackDepth];
811
812   while (attr) {
813
814      if (MESA_VERBOSE & VERBOSE_API) {
815         _mesa_debug(ctx, "glPopAttrib %s\n",
816                     _mesa_lookup_enum_by_nr(attr->kind));
817      }
818
819      switch (attr->kind) {
820         case GL_ACCUM_BUFFER_BIT:
821            {
822               const struct gl_accum_attrib *accum;
823               accum = (const struct gl_accum_attrib *) attr->data;
824               _mesa_ClearAccum(accum->ClearColor[0],
825                                accum->ClearColor[1],
826                                accum->ClearColor[2],
827                                accum->ClearColor[3]);
828            }
829            break;
830         case GL_COLOR_BUFFER_BIT:
831            {
832               const struct gl_colorbuffer_attrib *color;
833               color = (const struct gl_colorbuffer_attrib *) attr->data;
834               _mesa_ClearIndex((GLfloat) color->ClearIndex);
835               _mesa_ClearColor(color->ClearColor[0],
836                                color->ClearColor[1],
837                                color->ClearColor[2],
838                                color->ClearColor[3]);
839               _mesa_IndexMask(color->IndexMask);
840               _mesa_ColorMask((GLboolean) (color->ColorMask[0] != 0),
841                               (GLboolean) (color->ColorMask[1] != 0),
842                               (GLboolean) (color->ColorMask[2] != 0),
843                               (GLboolean) (color->ColorMask[3] != 0));
844#if 0
845               _mesa_DrawBuffersARB(ctx->Const.MaxDrawBuffers,
846                                    color->DrawBuffer);
847#else
848               _mesa_drawbuffers(ctx, ctx->Const.MaxDrawBuffers,
849                                 color->DrawBuffer, NULL);
850#endif
851               _mesa_set_enable(ctx, GL_ALPHA_TEST, color->AlphaEnabled);
852               _mesa_AlphaFunc(color->AlphaFunc, color->AlphaRef);
853               _mesa_set_enable(ctx, GL_BLEND, color->BlendEnabled);
854               _mesa_BlendFuncSeparateEXT(color->BlendSrcRGB,
855                                          color->BlendDstRGB,
856                                          color->BlendSrcA,
857                                          color->BlendDstA);
858	       /* This special case is because glBlendEquationSeparateEXT
859		* cannot take GL_LOGIC_OP as a parameter.
860		*/
861	       if ( color->BlendEquationRGB == color->BlendEquationA ) {
862		  _mesa_BlendEquation(color->BlendEquationRGB);
863	       }
864	       else {
865		  _mesa_BlendEquationSeparateEXT(color->BlendEquationRGB,
866						 color->BlendEquationA);
867	       }
868               _mesa_BlendColor(color->BlendColor[0],
869                                color->BlendColor[1],
870                                color->BlendColor[2],
871                                color->BlendColor[3]);
872               _mesa_LogicOp(color->LogicOp);
873               _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP,
874                                color->ColorLogicOpEnabled);
875               _mesa_set_enable(ctx, GL_INDEX_LOGIC_OP,
876                                color->IndexLogicOpEnabled);
877               _mesa_set_enable(ctx, GL_DITHER, color->DitherFlag);
878            }
879            break;
880         case GL_CURRENT_BIT:
881	    FLUSH_CURRENT( ctx, 0 );
882            MEMCPY( &ctx->Current, attr->data,
883		    sizeof(struct gl_current_attrib) );
884            break;
885         case GL_DEPTH_BUFFER_BIT:
886            {
887               const struct gl_depthbuffer_attrib *depth;
888               depth = (const struct gl_depthbuffer_attrib *) attr->data;
889               _mesa_DepthFunc(depth->Func);
890               _mesa_ClearDepth(depth->Clear);
891               _mesa_set_enable(ctx, GL_DEPTH_TEST, depth->Test);
892               _mesa_DepthMask(depth->Mask);
893               if (ctx->Extensions.HP_occlusion_test)
894                  _mesa_set_enable(ctx, GL_OCCLUSION_TEST_HP,
895                                   depth->OcclusionTest);
896            }
897            break;
898         case GL_ENABLE_BIT:
899            {
900               const struct gl_enable_attrib *enable;
901               enable = (const struct gl_enable_attrib *) attr->data;
902               pop_enable_group(ctx, enable);
903	       ctx->NewState |= _NEW_ALL;
904            }
905            break;
906         case GL_EVAL_BIT:
907            MEMCPY( &ctx->Eval, attr->data, sizeof(struct gl_eval_attrib) );
908	    ctx->NewState |= _NEW_EVAL;
909            break;
910         case GL_FOG_BIT:
911            {
912               const struct gl_fog_attrib *fog;
913               fog = (const struct gl_fog_attrib *) attr->data;
914               _mesa_set_enable(ctx, GL_FOG, fog->Enabled);
915               _mesa_Fogfv(GL_FOG_COLOR, fog->Color);
916               _mesa_Fogf(GL_FOG_DENSITY, fog->Density);
917               _mesa_Fogf(GL_FOG_START, fog->Start);
918               _mesa_Fogf(GL_FOG_END, fog->End);
919               _mesa_Fogf(GL_FOG_INDEX, fog->Index);
920               _mesa_Fogi(GL_FOG_MODE, fog->Mode);
921            }
922            break;
923         case GL_HINT_BIT:
924            {
925               const struct gl_hint_attrib *hint;
926               hint = (const struct gl_hint_attrib *) attr->data;
927               _mesa_Hint(GL_PERSPECTIVE_CORRECTION_HINT,
928                          hint->PerspectiveCorrection );
929               _mesa_Hint(GL_POINT_SMOOTH_HINT, hint->PointSmooth);
930               _mesa_Hint(GL_LINE_SMOOTH_HINT, hint->LineSmooth);
931               _mesa_Hint(GL_POLYGON_SMOOTH_HINT, hint->PolygonSmooth);
932               _mesa_Hint(GL_FOG_HINT, hint->Fog);
933               _mesa_Hint(GL_CLIP_VOLUME_CLIPPING_HINT_EXT,
934                          hint->ClipVolumeClipping);
935               if (ctx->Extensions.ARB_texture_compression)
936                  _mesa_Hint(GL_TEXTURE_COMPRESSION_HINT_ARB,
937                             hint->TextureCompression);
938            }
939            break;
940         case GL_LIGHTING_BIT:
941            {
942               GLuint i;
943               const struct gl_light_attrib *light;
944               light = (const struct gl_light_attrib *) attr->data;
945               /* lighting enable */
946               _mesa_set_enable(ctx, GL_LIGHTING, light->Enabled);
947               /* per-light state */
948               if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
949                  _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
950
951               for (i = 0; i < MAX_LIGHTS; i++) {
952                  GLenum lgt = (GLenum) (GL_LIGHT0 + i);
953		  const struct gl_light *l = &light->Light[i];
954		  GLfloat tmp[4];
955                  _mesa_set_enable(ctx, lgt, l->Enabled);
956		  _mesa_Lightfv( lgt, GL_AMBIENT, l->Ambient );
957		  _mesa_Lightfv( lgt, GL_DIFFUSE, l->Diffuse );
958		  _mesa_Lightfv( lgt, GL_SPECULAR, l->Specular );
959		  TRANSFORM_POINT( tmp, ctx->ModelviewMatrixStack.Top->inv, l->EyePosition );
960		  _mesa_Lightfv( lgt, GL_POSITION, tmp );
961		  TRANSFORM_POINT( tmp, ctx->ModelviewMatrixStack.Top->m, l->EyeDirection );
962		  _mesa_Lightfv( lgt, GL_SPOT_DIRECTION, tmp );
963		  _mesa_Lightfv( lgt, GL_SPOT_EXPONENT, &l->SpotExponent );
964		  _mesa_Lightfv( lgt, GL_SPOT_CUTOFF, &l->SpotCutoff );
965		  _mesa_Lightfv( lgt, GL_CONSTANT_ATTENUATION,
966				 &l->ConstantAttenuation );
967		  _mesa_Lightfv( lgt, GL_LINEAR_ATTENUATION,
968				 &l->LinearAttenuation );
969		  _mesa_Lightfv( lgt, GL_QUADRATIC_ATTENUATION,
970				 &l->QuadraticAttenuation );
971               }
972               /* light model */
973               _mesa_LightModelfv(GL_LIGHT_MODEL_AMBIENT,
974                                  light->Model.Ambient);
975               _mesa_LightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,
976                                 (GLfloat) light->Model.LocalViewer);
977               _mesa_LightModelf(GL_LIGHT_MODEL_TWO_SIDE,
978                                 (GLfloat) light->Model.TwoSide);
979               _mesa_LightModelf(GL_LIGHT_MODEL_COLOR_CONTROL,
980                                 (GLfloat) light->Model.ColorControl);
981               /* materials */
982               MEMCPY(&ctx->Light.Material, &light->Material,
983                      sizeof(struct gl_material));
984               /* shade model */
985               _mesa_ShadeModel(light->ShadeModel);
986               /* color material */
987               _mesa_ColorMaterial(light->ColorMaterialFace,
988                                   light->ColorMaterialMode);
989               _mesa_set_enable(ctx, GL_COLOR_MATERIAL,
990                                light->ColorMaterialEnabled);
991            }
992            break;
993         case GL_LINE_BIT:
994            {
995               const struct gl_line_attrib *line;
996               line = (const struct gl_line_attrib *) attr->data;
997               _mesa_set_enable(ctx, GL_LINE_SMOOTH, line->SmoothFlag);
998               _mesa_set_enable(ctx, GL_LINE_STIPPLE, line->StippleFlag);
999               _mesa_LineStipple(line->StippleFactor, line->StipplePattern);
1000               _mesa_LineWidth(line->Width);
1001            }
1002            break;
1003         case GL_LIST_BIT:
1004            MEMCPY( &ctx->List, attr->data, sizeof(struct gl_list_attrib) );
1005            break;
1006         case GL_PIXEL_MODE_BIT:
1007            MEMCPY( &ctx->Pixel, attr->data, sizeof(struct gl_pixel_attrib) );
1008            /* XXX what other pixel state needs to be set by function calls? */
1009            _mesa_ReadBuffer(ctx->Pixel.ReadBuffer);
1010	    ctx->NewState |= _NEW_PIXEL;
1011            break;
1012         case GL_POINT_BIT:
1013            {
1014               const struct gl_point_attrib *point;
1015               point = (const struct gl_point_attrib *) attr->data;
1016               _mesa_PointSize(point->Size);
1017               _mesa_set_enable(ctx, GL_POINT_SMOOTH, point->SmoothFlag);
1018               if (ctx->Extensions.EXT_point_parameters) {
1019                  _mesa_PointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT,
1020                                            point->Params);
1021                  _mesa_PointParameterfEXT(GL_POINT_SIZE_MIN_EXT,
1022                                           point->MinSize);
1023                  _mesa_PointParameterfEXT(GL_POINT_SIZE_MAX_EXT,
1024                                           point->MaxSize);
1025                  _mesa_PointParameterfEXT(GL_POINT_FADE_THRESHOLD_SIZE_EXT,
1026                                           point->Threshold);
1027               }
1028               if (ctx->Extensions.NV_point_sprite
1029		   || ctx->Extensions.ARB_point_sprite) {
1030                  GLuint u;
1031                  for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
1032                     _mesa_TexEnvi(GL_POINT_SPRITE_NV, GL_COORD_REPLACE_NV,
1033                                   (GLint) point->CoordReplace[u]);
1034                  }
1035                  _mesa_set_enable(ctx, GL_POINT_SPRITE_NV,point->PointSprite);
1036                  _mesa_PointParameteriNV(GL_POINT_SPRITE_R_MODE_NV,
1037                                          ctx->Point.SpriteRMode);
1038                  _mesa_PointParameterfEXT(GL_POINT_SPRITE_COORD_ORIGIN,
1039                                           (GLfloat)ctx->Point.SpriteOrigin);
1040               }
1041            }
1042            break;
1043         case GL_POLYGON_BIT:
1044            {
1045               const struct gl_polygon_attrib *polygon;
1046               polygon = (const struct gl_polygon_attrib *) attr->data;
1047               _mesa_CullFace(polygon->CullFaceMode);
1048               _mesa_FrontFace(polygon->FrontFace);
1049               _mesa_PolygonMode(GL_FRONT, polygon->FrontMode);
1050               _mesa_PolygonMode(GL_BACK, polygon->BackMode);
1051               _mesa_PolygonOffset(polygon->OffsetFactor,
1052                                   polygon->OffsetUnits);
1053               _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, polygon->SmoothFlag);
1054               _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, polygon->StippleFlag);
1055               _mesa_set_enable(ctx, GL_CULL_FACE, polygon->CullFlag);
1056               _mesa_set_enable(ctx, GL_POLYGON_OFFSET_POINT,
1057                                polygon->OffsetPoint);
1058               _mesa_set_enable(ctx, GL_POLYGON_OFFSET_LINE,
1059                                polygon->OffsetLine);
1060               _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL,
1061                                polygon->OffsetFill);
1062            }
1063            break;
1064	 case GL_POLYGON_STIPPLE_BIT:
1065	    MEMCPY( ctx->PolygonStipple, attr->data, 32*sizeof(GLuint) );
1066	    ctx->NewState |= _NEW_POLYGONSTIPPLE;
1067	    if (ctx->Driver.PolygonStipple)
1068	       ctx->Driver.PolygonStipple( ctx, (const GLubyte *) attr->data );
1069	    break;
1070         case GL_SCISSOR_BIT:
1071            {
1072               const struct gl_scissor_attrib *scissor;
1073               scissor = (const struct gl_scissor_attrib *) attr->data;
1074               _mesa_Scissor(scissor->X, scissor->Y,
1075                             scissor->Width, scissor->Height);
1076               _mesa_set_enable(ctx, GL_SCISSOR_TEST, scissor->Enabled);
1077            }
1078            break;
1079         case GL_STENCIL_BUFFER_BIT:
1080            {
1081               GLint face;
1082               const struct gl_stencil_attrib *stencil;
1083               stencil = (const struct gl_stencil_attrib *) attr->data;
1084               _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
1085               _mesa_ClearStencil(stencil->Clear);
1086               face = stencil->ActiveFace;
1087               if (ctx->Extensions.EXT_stencil_two_side) {
1088                  _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT, stencil->TestTwoSide);
1089                  face ^= 1;
1090               }
1091               do {
1092                  _mesa_ActiveStencilFaceEXT(face);
1093                  _mesa_StencilFunc(stencil->Function[face], stencil->Ref[face],
1094                                    stencil->ValueMask[face]);
1095                  _mesa_StencilMask(stencil->WriteMask[face]);
1096                  _mesa_StencilOp(stencil->FailFunc[face],
1097                                  stencil->ZFailFunc[face],
1098                                  stencil->ZPassFunc[face]);
1099                  face ^= 1;
1100               } while (face != (stencil->ActiveFace ^ 1));
1101            }
1102            break;
1103         case GL_TRANSFORM_BIT:
1104            {
1105               GLuint i;
1106               const struct gl_transform_attrib *xform;
1107               xform = (const struct gl_transform_attrib *) attr->data;
1108               _mesa_MatrixMode(xform->MatrixMode);
1109               if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
1110                  _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
1111
1112               /* restore clip planes */
1113               for (i = 0; i < MAX_CLIP_PLANES; i++) {
1114                  const GLuint mask = 1 << 1;
1115                  const GLfloat *eyePlane = xform->EyeUserPlane[i];
1116                  COPY_4V(ctx->Transform.EyeUserPlane[i], eyePlane);
1117                  if (xform->ClipPlanesEnabled & mask) {
1118                     _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
1119                  }
1120                  else {
1121                     _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
1122                  }
1123                  if (ctx->Driver.ClipPlane)
1124                     ctx->Driver.ClipPlane( ctx, GL_CLIP_PLANE0 + i, eyePlane );
1125               }
1126
1127               /* normalize/rescale */
1128               if (xform->Normalize != ctx->Transform.Normalize)
1129                  _mesa_set_enable(ctx, GL_NORMALIZE,ctx->Transform.Normalize);
1130               if (xform->RescaleNormals != ctx->Transform.RescaleNormals)
1131                  _mesa_set_enable(ctx, GL_RESCALE_NORMAL_EXT,
1132                                   ctx->Transform.RescaleNormals);
1133            }
1134            break;
1135         case GL_TEXTURE_BIT:
1136            /* Take care of texture object reference counters */
1137            {
1138               const struct gl_texture_attrib *texture;
1139               texture = (const struct gl_texture_attrib *) attr->data;
1140               pop_texture_group(ctx, texture);
1141	       ctx->NewState |= _NEW_TEXTURE;
1142            }
1143            break;
1144         case GL_VIEWPORT_BIT:
1145            {
1146               const struct gl_viewport_attrib *vp;
1147               vp = (const struct gl_viewport_attrib *) attr->data;
1148               _mesa_Viewport(vp->X, vp->Y, vp->Width, vp->Height);
1149               _mesa_DepthRange(vp->Near, vp->Far);
1150            }
1151            break;
1152         case GL_MULTISAMPLE_BIT_ARB:
1153            {
1154               const struct gl_multisample_attrib *ms;
1155               ms = (const struct gl_multisample_attrib *) attr->data;
1156               _mesa_SampleCoverageARB(ms->SampleCoverageValue,
1157                                       ms->SampleCoverageInvert);
1158            }
1159            break;
1160
1161         default:
1162            _mesa_problem( ctx, "Bad attrib flag in PopAttrib");
1163            break;
1164      }
1165
1166      next = attr->next;
1167      FREE( attr->data );
1168      FREE( attr );
1169      attr = next;
1170   }
1171}
1172
1173
1174/**
1175 * Helper for incrementing/decrementing vertex buffer object reference
1176 * counts when pushing/popping the GL_CLIENT_VERTEX_ARRAY_BIT attribute group.
1177 */
1178static void
1179adjust_buffer_object_ref_counts(struct gl_array_attrib *array, GLint step)
1180{
1181   GLuint i;
1182   array->Vertex.BufferObj->RefCount += step;
1183   array->Normal.BufferObj->RefCount += step;
1184   array->Color.BufferObj->RefCount += step;
1185   array->SecondaryColor.BufferObj->RefCount += step;
1186   array->FogCoord.BufferObj->RefCount += step;
1187   array->Index.BufferObj->RefCount += step;
1188   array->EdgeFlag.BufferObj->RefCount += step;
1189   for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++)
1190      array->TexCoord[i].BufferObj->RefCount += step;
1191   for (i = 0; i < VERT_ATTRIB_MAX; i++)
1192      array->VertexAttrib[i].BufferObj->RefCount += step;
1193
1194   array->ArrayBufferObj->RefCount += step;
1195   array->ElementArrayBufferObj->RefCount += step;
1196}
1197
1198
1199#define GL_CLIENT_PACK_BIT (1<<20)
1200#define GL_CLIENT_UNPACK_BIT (1<<21)
1201
1202
1203void GLAPIENTRY
1204_mesa_PushClientAttrib(GLbitfield mask)
1205{
1206   struct gl_attrib_node *newnode;
1207   struct gl_attrib_node *head;
1208
1209   GET_CURRENT_CONTEXT(ctx);
1210   ASSERT_OUTSIDE_BEGIN_END(ctx);
1211
1212   if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
1213      _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushClientAttrib" );
1214      return;
1215   }
1216
1217   /* Build linked list of attribute nodes which save all attribute */
1218   /* groups specified by the mask. */
1219   head = NULL;
1220
1221   if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
1222      struct gl_pixelstore_attrib *attr;
1223#if FEATURE_EXT_pixel_buffer_object
1224      ctx->Pack.BufferObj->RefCount++;
1225      ctx->Unpack.BufferObj->RefCount++;
1226#endif
1227      /* packing attribs */
1228      attr = MALLOC_STRUCT( gl_pixelstore_attrib );
1229      MEMCPY( attr, &ctx->Pack, sizeof(struct gl_pixelstore_attrib) );
1230      newnode = new_attrib_node( GL_CLIENT_PACK_BIT );
1231      newnode->data = attr;
1232      newnode->next = head;
1233      head = newnode;
1234      /* unpacking attribs */
1235      attr = MALLOC_STRUCT( gl_pixelstore_attrib );
1236      MEMCPY( attr, &ctx->Unpack, sizeof(struct gl_pixelstore_attrib) );
1237      newnode = new_attrib_node( GL_CLIENT_UNPACK_BIT );
1238      newnode->data = attr;
1239      newnode->next = head;
1240      head = newnode;
1241   }
1242   if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
1243      struct gl_array_attrib *attr;
1244      attr = MALLOC_STRUCT( gl_array_attrib );
1245      MEMCPY( attr, &ctx->Array, sizeof(struct gl_array_attrib) );
1246      newnode = new_attrib_node( GL_CLIENT_VERTEX_ARRAY_BIT );
1247      newnode->data = attr;
1248      newnode->next = head;
1249      head = newnode;
1250      /* bump reference counts on buffer objects */
1251      adjust_buffer_object_ref_counts(&ctx->Array, 1);
1252   }
1253
1254   ctx->ClientAttribStack[ctx->ClientAttribStackDepth] = head;
1255   ctx->ClientAttribStackDepth++;
1256}
1257
1258
1259
1260
1261void GLAPIENTRY
1262_mesa_PopClientAttrib(void)
1263{
1264   struct gl_attrib_node *attr, *next;
1265
1266   GET_CURRENT_CONTEXT(ctx);
1267   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1268
1269   if (ctx->ClientAttribStackDepth == 0) {
1270      _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib" );
1271      return;
1272   }
1273
1274   ctx->ClientAttribStackDepth--;
1275   attr = ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
1276
1277   while (attr) {
1278      switch (attr->kind) {
1279         case GL_CLIENT_PACK_BIT:
1280#if FEATURE_EXT_pixel_buffer_object
1281            ctx->Pack.BufferObj->RefCount--;
1282            if (ctx->Pack.BufferObj->RefCount <= 0) {
1283               _mesa_remove_buffer_object( ctx, ctx->Pack.BufferObj );
1284               (*ctx->Driver.DeleteBuffer)( ctx, ctx->Pack.BufferObj );
1285            }
1286#endif
1287            MEMCPY( &ctx->Pack, attr->data,
1288                    sizeof(struct gl_pixelstore_attrib) );
1289	    ctx->NewState |= _NEW_PACKUNPACK;
1290            break;
1291         case GL_CLIENT_UNPACK_BIT:
1292#if FEATURE_EXT_pixel_buffer_object
1293            ctx->Unpack.BufferObj->RefCount--;
1294            if (ctx->Unpack.BufferObj->RefCount <= 0) {
1295               _mesa_remove_buffer_object( ctx, ctx->Unpack.BufferObj );
1296               (*ctx->Driver.DeleteBuffer)( ctx, ctx->Unpack.BufferObj );
1297            }
1298#endif
1299            MEMCPY( &ctx->Unpack, attr->data,
1300                    sizeof(struct gl_pixelstore_attrib) );
1301	    ctx->NewState |= _NEW_PACKUNPACK;
1302            break;
1303         case GL_CLIENT_VERTEX_ARRAY_BIT:
1304            adjust_buffer_object_ref_counts(&ctx->Array, -1);
1305            MEMCPY( &ctx->Array, attr->data,
1306		    sizeof(struct gl_array_attrib) );
1307            /* decrement reference counts on buffer objects */
1308	    ctx->NewState |= _NEW_ARRAY;
1309            break;
1310         default:
1311            _mesa_problem( ctx, "Bad attrib flag in PopClientAttrib");
1312            break;
1313      }
1314
1315      next = attr->next;
1316      FREE( attr->data );
1317      FREE( attr );
1318      attr = next;
1319   }
1320}
1321
1322
1323void _mesa_init_attrib( GLcontext *ctx )
1324{
1325   /* Renderer and client attribute stacks */
1326   ctx->AttribStackDepth = 0;
1327   ctx->ClientAttribStackDepth = 0;
1328}
1329