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