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