signal _NEW_TEXTURE in all teximage functions
[mesa.git] / src / mesa / main / attrib.c
1 /* $Id: attrib.c,v 1.44 2001/02/20 16:42:25 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "accum.h"
33 #include "alpha.h"
34 #include "attrib.h"
35 #include "blend.h"
36 #include "buffers.h"
37 #include "clip.h"
38 #include "colormac.h"
39 #include "context.h"
40 #include "depth.h"
41 #include "enable.h"
42 #include "enums.h"
43 #include "fog.h"
44 #include "hint.h"
45 #include "light.h"
46 #include "lines.h"
47 #include "logic.h"
48 #include "masking.h"
49 #include "matrix.h"
50 #include "mem.h"
51 #include "points.h"
52 #include "polygon.h"
53 #include "scissor.h"
54 #include "simple_list.h"
55 #include "stencil.h"
56 #include "texstate.h"
57 #include "mtypes.h"
58 #endif
59
60
61
62
63 /*
64 * Allocate a new attribute state node. These nodes have a
65 * "kind" value and a pointer to a struct of state data.
66 */
67 static struct gl_attrib_node *
68 new_attrib_node( GLbitfield kind )
69 {
70 struct gl_attrib_node *an = MALLOC_STRUCT(gl_attrib_node);
71 if (an) {
72 an->kind = kind;
73 }
74 return an;
75 }
76
77
78
79 /*
80 * Copy texture object state from one texture object to another.
81 */
82 static void
83 copy_texobj_state( struct gl_texture_object *dest,
84 const struct gl_texture_object *src )
85 {
86 /*
87 dest->Name = src->Name;
88 dest->Dimensions = src->Dimensions;
89 */
90 dest->Priority = src->Priority;
91 dest->BorderColor[0] = src->BorderColor[0];
92 dest->BorderColor[1] = src->BorderColor[1];
93 dest->BorderColor[2] = src->BorderColor[2];
94 dest->BorderColor[3] = src->BorderColor[3];
95 dest->WrapS = src->WrapS;
96 dest->WrapT = src->WrapT;
97 dest->WrapR = src->WrapR;
98 dest->MinFilter = src->MinFilter;
99 dest->MagFilter = src->MagFilter;
100 dest->MinLod = src->MinLod;
101 dest->MaxLod = src->MaxLod;
102 dest->BaseLevel = src->BaseLevel;
103 dest->MaxLevel = src->MaxLevel;
104 dest->CompareFlag = src->CompareFlag;
105 dest->CompareOperator = src->CompareOperator;
106 dest->ShadowAmbient = src->ShadowAmbient;
107 dest->_MaxLevel = src->_MaxLevel;
108 dest->_MaxLambda = src->_MaxLambda;
109 dest->Palette = src->Palette;
110 dest->Complete = src->Complete;
111 }
112
113
114
115 void
116 _mesa_PushAttrib(GLbitfield mask)
117 {
118 struct gl_attrib_node *newnode;
119 struct gl_attrib_node *head;
120
121 GET_CURRENT_CONTEXT(ctx);
122 ASSERT_OUTSIDE_BEGIN_END(ctx);
123
124 if (MESA_VERBOSE&VERBOSE_API)
125 fprintf(stderr, "glPushAttrib %x\n", (int)mask);
126
127 if (ctx->AttribStackDepth >= MAX_ATTRIB_STACK_DEPTH) {
128 gl_error( ctx, GL_STACK_OVERFLOW, "glPushAttrib" );
129 return;
130 }
131
132 /* Build linked list of attribute nodes which save all attribute */
133 /* groups specified by the mask. */
134 head = NULL;
135
136 if (mask & GL_ACCUM_BUFFER_BIT) {
137 struct gl_accum_attrib *attr;
138 attr = MALLOC_STRUCT( gl_accum_attrib );
139 MEMCPY( attr, &ctx->Accum, sizeof(struct gl_accum_attrib) );
140 newnode = new_attrib_node( GL_ACCUM_BUFFER_BIT );
141 newnode->data = attr;
142 newnode->next = head;
143 head = newnode;
144 }
145
146 if (mask & GL_COLOR_BUFFER_BIT) {
147 struct gl_colorbuffer_attrib *attr;
148 attr = MALLOC_STRUCT( gl_colorbuffer_attrib );
149 MEMCPY( attr, &ctx->Color, sizeof(struct gl_colorbuffer_attrib) );
150 newnode = new_attrib_node( GL_COLOR_BUFFER_BIT );
151 newnode->data = attr;
152 newnode->next = head;
153 head = newnode;
154 }
155
156 if (mask & GL_CURRENT_BIT) {
157 struct gl_current_attrib *attr;
158 FLUSH_CURRENT( ctx, 0 );
159 attr = MALLOC_STRUCT( gl_current_attrib );
160 MEMCPY( attr, &ctx->Current, sizeof(struct gl_current_attrib) );
161 newnode = new_attrib_node( GL_CURRENT_BIT );
162 newnode->data = attr;
163 newnode->next = head;
164 head = newnode;
165 }
166
167 if (mask & GL_DEPTH_BUFFER_BIT) {
168 struct gl_depthbuffer_attrib *attr;
169 attr = MALLOC_STRUCT( gl_depthbuffer_attrib );
170 MEMCPY( attr, &ctx->Depth, sizeof(struct gl_depthbuffer_attrib) );
171 newnode = new_attrib_node( GL_DEPTH_BUFFER_BIT );
172 newnode->data = attr;
173 newnode->next = head;
174 head = newnode;
175 }
176
177 if (mask & GL_ENABLE_BIT) {
178 struct gl_enable_attrib *attr;
179 GLuint i;
180 attr = MALLOC_STRUCT( gl_enable_attrib );
181 /* Copy enable flags from all other attributes into the enable struct. */
182 attr->AlphaTest = ctx->Color.AlphaEnabled;
183 attr->AutoNormal = ctx->Eval.AutoNormal;
184 attr->Blend = ctx->Color.BlendEnabled;
185 for (i=0;i<MAX_CLIP_PLANES;i++) {
186 attr->ClipPlane[i] = ctx->Transform.ClipEnabled[i];
187 }
188 attr->ColorMaterial = ctx->Light.ColorMaterialEnabled;
189 attr->Convolution1D = ctx->Pixel.Convolution1DEnabled;
190 attr->Convolution2D = ctx->Pixel.Convolution2DEnabled;
191 attr->Separable2D = ctx->Pixel.Separable2DEnabled;
192 attr->CullFace = ctx->Polygon.CullFlag;
193 attr->DepthTest = ctx->Depth.Test;
194 attr->Dither = ctx->Color.DitherFlag;
195 attr->Fog = ctx->Fog.Enabled;
196 for (i=0;i<MAX_LIGHTS;i++) {
197 attr->Light[i] = ctx->Light.Light[i].Enabled;
198 }
199 attr->Lighting = ctx->Light.Enabled;
200 attr->LineSmooth = ctx->Line.SmoothFlag;
201 attr->LineStipple = ctx->Line.StippleFlag;
202 attr->Histogram = ctx->Pixel.HistogramEnabled;
203 attr->MinMax = ctx->Pixel.MinMaxEnabled;
204 attr->IndexLogicOp = ctx->Color.IndexLogicOpEnabled;
205 attr->ColorLogicOp = ctx->Color.ColorLogicOpEnabled;
206 attr->Map1Color4 = ctx->Eval.Map1Color4;
207 attr->Map1Index = ctx->Eval.Map1Index;
208 attr->Map1Normal = ctx->Eval.Map1Normal;
209 attr->Map1TextureCoord1 = ctx->Eval.Map1TextureCoord1;
210 attr->Map1TextureCoord2 = ctx->Eval.Map1TextureCoord2;
211 attr->Map1TextureCoord3 = ctx->Eval.Map1TextureCoord3;
212 attr->Map1TextureCoord4 = ctx->Eval.Map1TextureCoord4;
213 attr->Map1Vertex3 = ctx->Eval.Map1Vertex3;
214 attr->Map1Vertex4 = ctx->Eval.Map1Vertex4;
215 attr->Map2Color4 = ctx->Eval.Map2Color4;
216 attr->Map2Index = ctx->Eval.Map2Index;
217 attr->Map2Normal = ctx->Eval.Map2Normal;
218 attr->Map2TextureCoord1 = ctx->Eval.Map2TextureCoord1;
219 attr->Map2TextureCoord2 = ctx->Eval.Map2TextureCoord2;
220 attr->Map2TextureCoord3 = ctx->Eval.Map2TextureCoord3;
221 attr->Map2TextureCoord4 = ctx->Eval.Map2TextureCoord4;
222 attr->Map2Vertex3 = ctx->Eval.Map2Vertex3;
223 attr->Map2Vertex4 = ctx->Eval.Map2Vertex4;
224 attr->Normalize = ctx->Transform.Normalize;
225 attr->PixelTexture = ctx->Pixel.PixelTextureEnabled;
226 attr->PointSmooth = ctx->Point.SmoothFlag;
227 attr->PolygonOffsetPoint = ctx->Polygon.OffsetPoint;
228 attr->PolygonOffsetLine = ctx->Polygon.OffsetLine;
229 attr->PolygonOffsetFill = ctx->Polygon.OffsetFill;
230 attr->PolygonSmooth = ctx->Polygon.SmoothFlag;
231 attr->PolygonStipple = ctx->Polygon.StippleFlag;
232 attr->RescaleNormals = ctx->Transform.RescaleNormals;
233 attr->Scissor = ctx->Scissor.Enabled;
234 attr->Stencil = ctx->Stencil.Enabled;
235 for (i=0; i<MAX_TEXTURE_UNITS; i++) {
236 attr->Texture[i] = ctx->Texture.Unit[i].Enabled;
237 attr->TexGen[i] = ctx->Texture.Unit[i].TexGenEnabled;
238 }
239 newnode = new_attrib_node( GL_ENABLE_BIT );
240 newnode->data = attr;
241 newnode->next = head;
242 head = newnode;
243 }
244
245 if (mask & GL_EVAL_BIT) {
246 struct gl_eval_attrib *attr;
247 attr = MALLOC_STRUCT( gl_eval_attrib );
248 MEMCPY( attr, &ctx->Eval, sizeof(struct gl_eval_attrib) );
249 newnode = new_attrib_node( GL_EVAL_BIT );
250 newnode->data = attr;
251 newnode->next = head;
252 head = newnode;
253 }
254
255 if (mask & GL_FOG_BIT) {
256 struct gl_fog_attrib *attr;
257 attr = MALLOC_STRUCT( gl_fog_attrib );
258 MEMCPY( attr, &ctx->Fog, sizeof(struct gl_fog_attrib) );
259 newnode = new_attrib_node( GL_FOG_BIT );
260 newnode->data = attr;
261 newnode->next = head;
262 head = newnode;
263 }
264
265 if (mask & GL_HINT_BIT) {
266 struct gl_hint_attrib *attr;
267 attr = MALLOC_STRUCT( gl_hint_attrib );
268 MEMCPY( attr, &ctx->Hint, sizeof(struct gl_hint_attrib) );
269 newnode = new_attrib_node( GL_HINT_BIT );
270 newnode->data = attr;
271 newnode->next = head;
272 head = newnode;
273 }
274
275 if (mask & GL_LIGHTING_BIT) {
276 struct gl_light_attrib *attr;
277 FLUSH_CURRENT(ctx, 0); /* flush material changes */
278 attr = MALLOC_STRUCT( gl_light_attrib );
279 MEMCPY( attr, &ctx->Light, sizeof(struct gl_light_attrib) );
280 newnode = new_attrib_node( GL_LIGHTING_BIT );
281 newnode->data = attr;
282 newnode->next = head;
283 head = newnode;
284 }
285
286 if (mask & GL_LINE_BIT) {
287 struct gl_line_attrib *attr;
288 attr = MALLOC_STRUCT( gl_line_attrib );
289 MEMCPY( attr, &ctx->Line, sizeof(struct gl_line_attrib) );
290 newnode = new_attrib_node( GL_LINE_BIT );
291 newnode->data = attr;
292 newnode->next = head;
293 head = newnode;
294 }
295
296 if (mask & GL_LIST_BIT) {
297 struct gl_list_attrib *attr;
298 attr = MALLOC_STRUCT( gl_list_attrib );
299 MEMCPY( attr, &ctx->List, sizeof(struct gl_list_attrib) );
300 newnode = new_attrib_node( GL_LIST_BIT );
301 newnode->data = attr;
302 newnode->next = head;
303 head = newnode;
304 }
305
306 if (mask & GL_PIXEL_MODE_BIT) {
307 struct gl_pixel_attrib *attr;
308 attr = MALLOC_STRUCT( gl_pixel_attrib );
309 MEMCPY( attr, &ctx->Pixel, sizeof(struct gl_pixel_attrib) );
310 newnode = new_attrib_node( GL_PIXEL_MODE_BIT );
311 newnode->data = attr;
312 newnode->next = head;
313 head = newnode;
314 }
315
316 if (mask & GL_POINT_BIT) {
317 struct gl_point_attrib *attr;
318 attr = MALLOC_STRUCT( gl_point_attrib );
319 MEMCPY( attr, &ctx->Point, sizeof(struct gl_point_attrib) );
320 newnode = new_attrib_node( GL_POINT_BIT );
321 newnode->data = attr;
322 newnode->next = head;
323 head = newnode;
324 }
325
326 if (mask & GL_POLYGON_BIT) {
327 struct gl_polygon_attrib *attr;
328 attr = MALLOC_STRUCT( gl_polygon_attrib );
329 MEMCPY( attr, &ctx->Polygon, sizeof(struct gl_polygon_attrib) );
330 newnode = new_attrib_node( GL_POLYGON_BIT );
331 newnode->data = attr;
332 newnode->next = head;
333 head = newnode;
334 }
335
336 if (mask & GL_POLYGON_STIPPLE_BIT) {
337 GLuint *stipple;
338 stipple = (GLuint *) MALLOC( 32*sizeof(GLuint) );
339 MEMCPY( stipple, ctx->PolygonStipple, 32*sizeof(GLuint) );
340 newnode = new_attrib_node( GL_POLYGON_STIPPLE_BIT );
341 newnode->data = stipple;
342 newnode->next = head;
343 head = newnode;
344 }
345
346 if (mask & GL_SCISSOR_BIT) {
347 struct gl_scissor_attrib *attr;
348 attr = MALLOC_STRUCT( gl_scissor_attrib );
349 MEMCPY( attr, &ctx->Scissor, sizeof(struct gl_scissor_attrib) );
350 newnode = new_attrib_node( GL_SCISSOR_BIT );
351 newnode->data = attr;
352 newnode->next = head;
353 head = newnode;
354 }
355
356 if (mask & GL_STENCIL_BUFFER_BIT) {
357 struct gl_stencil_attrib *attr;
358 attr = MALLOC_STRUCT( gl_stencil_attrib );
359 MEMCPY( attr, &ctx->Stencil, sizeof(struct gl_stencil_attrib) );
360 newnode = new_attrib_node( GL_STENCIL_BUFFER_BIT );
361 newnode->data = attr;
362 newnode->next = head;
363 head = newnode;
364 }
365
366 if (mask & GL_TEXTURE_BIT) {
367 struct gl_texture_attrib *attr;
368 GLuint u;
369 /* Take care of texture object reference counters */
370 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
371 ctx->Texture.Unit[u].Current1D->RefCount++;
372 ctx->Texture.Unit[u].Current2D->RefCount++;
373 ctx->Texture.Unit[u].Current3D->RefCount++;
374 ctx->Texture.Unit[u].CurrentCubeMap->RefCount++;
375 }
376 attr = MALLOC_STRUCT( gl_texture_attrib );
377 MEMCPY( attr, &ctx->Texture, sizeof(struct gl_texture_attrib) );
378 /* copy state of the currently bound texture objects */
379 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
380 copy_texobj_state(&attr->Unit[u].Saved1D, attr->Unit[u].Current1D);
381 copy_texobj_state(&attr->Unit[u].Saved2D, attr->Unit[u].Current2D);
382 copy_texobj_state(&attr->Unit[u].Saved3D, attr->Unit[u].Current3D);
383 copy_texobj_state(&attr->Unit[u].SavedCubeMap, attr->Unit[u].CurrentCubeMap);
384 }
385 newnode = new_attrib_node( GL_TEXTURE_BIT );
386 newnode->data = attr;
387 newnode->next = head;
388 head = newnode;
389 }
390
391 if (mask & GL_TRANSFORM_BIT) {
392 struct gl_transform_attrib *attr;
393 attr = MALLOC_STRUCT( gl_transform_attrib );
394 MEMCPY( attr, &ctx->Transform, sizeof(struct gl_transform_attrib) );
395 newnode = new_attrib_node( GL_TRANSFORM_BIT );
396 newnode->data = attr;
397 newnode->next = head;
398 head = newnode;
399 }
400
401 if (mask & GL_VIEWPORT_BIT) {
402 struct gl_viewport_attrib *attr;
403 attr = MALLOC_STRUCT( gl_viewport_attrib );
404 MEMCPY( attr, &ctx->Viewport, sizeof(struct gl_viewport_attrib) );
405 newnode = new_attrib_node( GL_VIEWPORT_BIT );
406 newnode->data = attr;
407 newnode->next = head;
408 head = newnode;
409 }
410
411 ctx->AttribStack[ctx->AttribStackDepth] = head;
412 ctx->AttribStackDepth++;
413 }
414
415
416
417 static void
418 pop_enable_group(GLcontext *ctx, const struct gl_enable_attrib *enable)
419 {
420 GLuint i;
421
422 #define TEST_AND_UPDATE(VALUE, NEWVALUE, ENUM) \
423 if ((VALUE) != (NEWVALUE)) { \
424 _mesa_set_enable( ctx, ENUM, (NEWVALUE) ); \
425 }
426
427 TEST_AND_UPDATE(ctx->Color.AlphaEnabled, enable->AlphaTest, GL_ALPHA_TEST);
428 TEST_AND_UPDATE(ctx->Transform.Normalize, enable->AutoNormal, GL_NORMALIZE);
429 TEST_AND_UPDATE(ctx->Color.BlendEnabled, enable->Blend, GL_BLEND);
430
431 for (i=0;i<MAX_CLIP_PLANES;i++) {
432 if (ctx->Transform.ClipEnabled[i] != enable->ClipPlane[i])
433 _mesa_set_enable(ctx, (GLenum) (GL_CLIP_PLANE0 + i),
434 enable->ClipPlane[i]);
435 }
436
437 TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled, enable->ColorMaterial,
438 GL_COLOR_MATERIAL);
439 TEST_AND_UPDATE(ctx->Polygon.CullFlag, enable->CullFace, GL_CULL_FACE);
440 TEST_AND_UPDATE(ctx->Depth.Test, enable->DepthTest, GL_DEPTH_TEST);
441 TEST_AND_UPDATE(ctx->Color.DitherFlag, enable->Dither, GL_DITHER);
442 TEST_AND_UPDATE(ctx->Pixel.Convolution1DEnabled, enable->Convolution1D,
443 GL_CONVOLUTION_1D);
444 TEST_AND_UPDATE(ctx->Pixel.Convolution2DEnabled, enable->Convolution2D,
445 GL_CONVOLUTION_2D);
446 TEST_AND_UPDATE(ctx->Pixel.Separable2DEnabled, enable->Separable2D,
447 GL_SEPARABLE_2D);
448 TEST_AND_UPDATE(ctx->Fog.Enabled, enable->Fog, GL_FOG);
449 TEST_AND_UPDATE(ctx->Light.Enabled, enable->Lighting, GL_LIGHTING);
450 TEST_AND_UPDATE(ctx->Line.SmoothFlag, enable->LineSmooth, GL_LINE_SMOOTH);
451 TEST_AND_UPDATE(ctx->Line.StippleFlag, enable->LineStipple,
452 GL_LINE_STIPPLE);
453 TEST_AND_UPDATE(ctx->Color.IndexLogicOpEnabled, enable->IndexLogicOp,
454 GL_INDEX_LOGIC_OP);
455 TEST_AND_UPDATE(ctx->Color.ColorLogicOpEnabled, enable->ColorLogicOp,
456 GL_COLOR_LOGIC_OP);
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 TEST_AND_UPDATE(ctx->Eval.Map2Color4, enable->Map2Color4, GL_MAP2_COLOR_4);
473 TEST_AND_UPDATE(ctx->Eval.Map2Index, enable->Map2Index, GL_MAP2_INDEX);
474 TEST_AND_UPDATE(ctx->Eval.Map2Normal, enable->Map2Normal, GL_MAP2_NORMAL);
475 TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord1, enable->Map2TextureCoord1,
476 GL_MAP2_TEXTURE_COORD_1);
477 TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord2, enable->Map2TextureCoord2,
478 GL_MAP2_TEXTURE_COORD_2);
479 TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord3, enable->Map2TextureCoord3,
480 GL_MAP2_TEXTURE_COORD_3);
481 TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord4, enable->Map2TextureCoord4,
482 GL_MAP2_TEXTURE_COORD_4);
483 TEST_AND_UPDATE(ctx->Eval.Map2Vertex3, enable->Map2Vertex3,
484 GL_MAP2_VERTEX_3);
485 TEST_AND_UPDATE(ctx->Eval.Map2Vertex4, enable->Map2Vertex4,
486 GL_MAP2_VERTEX_4);
487 TEST_AND_UPDATE(ctx->Transform.Normalize, enable->Normalize, GL_NORMALIZE);
488 TEST_AND_UPDATE(ctx->Transform.RescaleNormals, enable->RescaleNormals,
489 GL_RESCALE_NORMAL_EXT);
490 TEST_AND_UPDATE(ctx->Pixel.PixelTextureEnabled, enable->PixelTexture,
491 GL_POINT_SMOOTH);
492 TEST_AND_UPDATE(ctx->Point.SmoothFlag, enable->PointSmooth,
493 GL_POINT_SMOOTH);
494 TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, enable->PolygonOffsetPoint,
495 GL_POLYGON_OFFSET_POINT);
496 TEST_AND_UPDATE(ctx->Polygon.OffsetLine, enable->PolygonOffsetLine,
497 GL_POLYGON_OFFSET_LINE);
498 TEST_AND_UPDATE(ctx->Polygon.OffsetFill, enable->PolygonOffsetFill,
499 GL_POLYGON_OFFSET_FILL);
500 TEST_AND_UPDATE(ctx->Polygon.SmoothFlag, enable->PolygonSmooth,
501 GL_POLYGON_SMOOTH);
502 TEST_AND_UPDATE(ctx->Polygon.StippleFlag, enable->PolygonStipple,
503 GL_POLYGON_STIPPLE);
504 TEST_AND_UPDATE(ctx->Scissor.Enabled, enable->Scissor, GL_SCISSOR_TEST);
505 TEST_AND_UPDATE(ctx->Stencil.Enabled, enable->Stencil, GL_STENCIL_TEST);
506 #undef TEST_AND_UPDATE
507
508 /* texture unit enables */
509 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
510 if (ctx->Texture.Unit[i].Enabled != enable->Texture[i]) {
511 ctx->Texture.Unit[i].Enabled = enable->Texture[i];
512 if (ctx->Driver.Enable) {
513 if (ctx->Driver.ActiveTexture) {
514 (*ctx->Driver.ActiveTexture)(ctx, i);
515 }
516 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_1D,
517 (GLboolean) (enable->Texture[i] & TEXTURE0_1D) );
518 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_2D,
519 (GLboolean) (enable->Texture[i] & TEXTURE0_2D) );
520 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_3D,
521 (GLboolean) (enable->Texture[i] & TEXTURE0_3D) );
522 }
523 }
524
525 if (ctx->Texture.Unit[i].TexGenEnabled != enable->TexGen[i]) {
526 ctx->Texture.Unit[i].TexGenEnabled = enable->TexGen[i];
527 if (ctx->Driver.Enable) {
528 if (ctx->Driver.ActiveTexture) {
529 (*ctx->Driver.ActiveTexture)(ctx, i);
530 }
531 if (enable->TexGen[i] & S_BIT)
532 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_S, GL_TRUE);
533 else
534 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_S, GL_FALSE);
535 if (enable->TexGen[i] & T_BIT)
536 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_T, GL_TRUE);
537 else
538 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_T, GL_FALSE);
539 if (enable->TexGen[i] & R_BIT)
540 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_R, GL_TRUE);
541 else
542 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_R, GL_FALSE);
543 if (enable->TexGen[i] & Q_BIT)
544 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_Q, GL_TRUE);
545 else
546 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_Q, GL_FALSE);
547 }
548 }
549 }
550
551 if (ctx->Driver.ActiveTexture) {
552 (*ctx->Driver.ActiveTexture)(ctx, ctx->Texture.CurrentUnit);
553 }
554 }
555
556
557
558 /*
559 * This function is kind of long just because we have to call a lot
560 * of device driver functions to update device driver state.
561 *
562 * XXX As it is now, most of the pop-code calls immediate-mode Mesa functions
563 * in order to restore GL state. This isn't terribly efficient but it
564 * ensures that dirty flags and any derived state gets updated correctly.
565 * We could at least check if the value to restore equals the current value
566 * and then skip the Mesa call.
567 */
568 void
569 _mesa_PopAttrib(void)
570 {
571 struct gl_attrib_node *attr, *next;
572 GET_CURRENT_CONTEXT(ctx);
573 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
574
575 if (ctx->AttribStackDepth == 0) {
576 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopAttrib" );
577 return;
578 }
579
580 ctx->AttribStackDepth--;
581 attr = ctx->AttribStack[ctx->AttribStackDepth];
582
583 while (attr) {
584
585 if (MESA_VERBOSE&VERBOSE_API)
586 fprintf(stderr, "glPopAttrib %s\n", gl_lookup_enum_by_nr(attr->kind));
587
588 switch (attr->kind) {
589 case GL_ACCUM_BUFFER_BIT:
590 {
591 const struct gl_accum_attrib *accum;
592 accum = (const struct gl_accum_attrib *) attr->data;
593 _mesa_ClearAccum(accum->ClearColor[0],
594 accum->ClearColor[1],
595 accum->ClearColor[2],
596 accum->ClearColor[3]);
597 }
598 break;
599 case GL_COLOR_BUFFER_BIT:
600 {
601 const struct gl_colorbuffer_attrib *color;
602 color = (const struct gl_colorbuffer_attrib *) attr->data;
603 _mesa_ClearIndex(color->ClearIndex);
604 _mesa_ClearColor(CHAN_TO_FLOAT(color->ClearColor[0]),
605 CHAN_TO_FLOAT(color->ClearColor[1]),
606 CHAN_TO_FLOAT(color->ClearColor[2]),
607 CHAN_TO_FLOAT(color->ClearColor[3]));
608 _mesa_IndexMask(color->IndexMask);
609 _mesa_ColorMask((GLboolean) (color->ColorMask[0] != 0),
610 (GLboolean) (color->ColorMask[1] != 0),
611 (GLboolean) (color->ColorMask[2] != 0),
612 (GLboolean) (color->ColorMask[3] != 0));
613 _mesa_DrawBuffer(color->DrawBuffer);
614 _mesa_set_enable(ctx, GL_ALPHA_TEST, color->AlphaEnabled);
615 _mesa_AlphaFunc(color->AlphaFunc,
616 CHAN_TO_FLOAT(color->AlphaRef));
617 _mesa_set_enable(ctx, GL_BLEND, color->BlendEnabled);
618 _mesa_BlendFuncSeparateEXT(color->BlendSrcRGB,
619 color->BlendDstRGB,
620 color->BlendSrcA,
621 color->BlendDstA);
622 _mesa_BlendEquation(color->BlendEquation);
623 _mesa_BlendColor(color->BlendColor[0],
624 color->BlendColor[1],
625 color->BlendColor[2],
626 color->BlendColor[3]);
627 _mesa_LogicOp(color->LogicOp);
628 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP,
629 color->ColorLogicOpEnabled);
630 _mesa_set_enable(ctx, GL_INDEX_LOGIC_OP,
631 color->IndexLogicOpEnabled);
632 _mesa_set_enable(ctx, GL_DITHER, color->DitherFlag);
633 }
634 break;
635 case GL_CURRENT_BIT:
636 FLUSH_CURRENT( ctx, 0 );
637 MEMCPY( &ctx->Current, attr->data,
638 sizeof(struct gl_current_attrib) );
639 break;
640 case GL_DEPTH_BUFFER_BIT:
641 {
642 const struct gl_depthbuffer_attrib *depth;
643 depth = (const struct gl_depthbuffer_attrib *) attr->data;
644 _mesa_DepthFunc(depth->Func);
645 _mesa_ClearDepth(depth->Clear);
646 _mesa_set_enable(ctx, GL_DEPTH_TEST, depth->Test);
647 _mesa_DepthMask(depth->Mask);
648 if (ctx->Extensions.HP_occlusion_test)
649 _mesa_set_enable(ctx, GL_OCCLUSION_TEST_HP,
650 depth->OcclusionTest);
651 }
652 break;
653 case GL_ENABLE_BIT:
654 {
655 const struct gl_enable_attrib *enable;
656 enable = (const struct gl_enable_attrib *) attr->data;
657 pop_enable_group(ctx, enable);
658 ctx->NewState |= _NEW_ALL;
659 }
660 break;
661 case GL_EVAL_BIT:
662 MEMCPY( &ctx->Eval, attr->data, sizeof(struct gl_eval_attrib) );
663 ctx->NewState |= _NEW_EVAL;
664 break;
665 case GL_FOG_BIT:
666 {
667 const struct gl_fog_attrib *fog;
668 fog = (const struct gl_fog_attrib *) attr->data;
669 _mesa_set_enable(ctx, GL_FOG, fog->Enabled);
670 _mesa_Fogfv(GL_FOG_COLOR, fog->Color);
671 _mesa_Fogf(GL_FOG_DENSITY, fog->Density);
672 _mesa_Fogf(GL_FOG_START, fog->Start);
673 _mesa_Fogf(GL_FOG_END, fog->End);
674 _mesa_Fogf(GL_FOG_INDEX, fog->Index);
675 _mesa_Fogi(GL_FOG_MODE, fog->Mode);
676 }
677 break;
678 case GL_HINT_BIT:
679 {
680 const struct gl_hint_attrib *hint;
681 hint = (const struct gl_hint_attrib *) attr->data;
682 /* XXX this memcpy is temporary: */
683 MEMCPY(&ctx->Hint, hint, sizeof(struct gl_hint_attrib));
684 _mesa_Hint(GL_PERSPECTIVE_CORRECTION_HINT,
685 hint->PerspectiveCorrection );
686 _mesa_Hint(GL_POINT_SMOOTH_HINT, hint->PointSmooth);
687 _mesa_Hint(GL_LINE_SMOOTH_HINT, hint->LineSmooth);
688 _mesa_Hint(GL_POLYGON_SMOOTH_HINT, hint->PolygonSmooth);
689 _mesa_Hint(GL_FOG_HINT, hint->Fog);
690 _mesa_Hint(GL_CLIP_VOLUME_CLIPPING_HINT_EXT,
691 hint->ClipVolumeClipping);
692 if (ctx->Extensions.ARB_texture_compression)
693 _mesa_Hint(GL_TEXTURE_COMPRESSION_HINT_ARB,
694 hint->TextureCompression);
695 }
696 break;
697 case GL_LIGHTING_BIT:
698 {
699 GLuint i;
700 const struct gl_light_attrib *light;
701 light = (const struct gl_light_attrib *) attr->data;
702 /* lighting enable */
703 _mesa_set_enable(ctx, GL_LIGHTING, light->Enabled);
704 /* per-light state */
705 for (i = 0; i < MAX_LIGHTS; i++) {
706 GLenum lgt = (GLenum) (GL_LIGHT0 + i);
707 _mesa_set_enable(ctx, lgt, light->Light[i].Enabled);
708 MEMCPY(&ctx->Light.Light[i], &light->Light[i],
709 sizeof(struct gl_light));
710 }
711 /* light model */
712 _mesa_LightModelfv(GL_LIGHT_MODEL_AMBIENT,
713 light->Model.Ambient);
714 _mesa_LightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,
715 (GLfloat) light->Model.LocalViewer);
716 _mesa_LightModelf(GL_LIGHT_MODEL_TWO_SIDE,
717 (GLfloat) light->Model.TwoSide);
718 _mesa_LightModelf(GL_LIGHT_MODEL_COLOR_CONTROL,
719 (GLfloat) light->Model.ColorControl);
720 /* materials */
721 MEMCPY(ctx->Light.Material, light->Material,
722 2 * sizeof(struct gl_material));
723 /* shade model */
724 _mesa_ShadeModel(light->ShadeModel);
725 /* color material */
726 _mesa_ColorMaterial(light->ColorMaterialFace,
727 light->ColorMaterialMode);
728 _mesa_set_enable(ctx, GL_COLOR_MATERIAL,
729 light->ColorMaterialEnabled);
730 }
731 break;
732 case GL_LINE_BIT:
733 {
734 const struct gl_line_attrib *line;
735 line = (const struct gl_line_attrib *) attr->data;
736 _mesa_set_enable(ctx, GL_LINE_SMOOTH, line->SmoothFlag);
737 _mesa_set_enable(ctx, GL_LINE_STIPPLE, line->StippleFlag);
738 _mesa_LineStipple(line->StippleFactor, line->StipplePattern);
739 _mesa_LineWidth(line->Width);
740 }
741 break;
742 case GL_LIST_BIT:
743 MEMCPY( &ctx->List, attr->data, sizeof(struct gl_list_attrib) );
744 break;
745 case GL_PIXEL_MODE_BIT:
746 MEMCPY( &ctx->Pixel, attr->data, sizeof(struct gl_pixel_attrib) );
747 ctx->NewState |= _NEW_PIXEL;
748 break;
749 case GL_POINT_BIT:
750 {
751 const struct gl_point_attrib *point;
752 point = (const struct gl_point_attrib *) attr->data;
753 _mesa_PointSize(point->Size);
754 _mesa_set_enable(ctx, GL_POINT_SMOOTH, point->SmoothFlag);
755 _mesa_PointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT,
756 point->Params);
757 _mesa_PointParameterfEXT(GL_POINT_SIZE_MIN_EXT, point->MinSize);
758 _mesa_PointParameterfEXT(GL_POINT_SIZE_MAX_EXT, point->MaxSize);
759 _mesa_PointParameterfEXT(GL_POINT_FADE_THRESHOLD_SIZE_EXT,
760 point->Threshold);
761 }
762 break;
763 case GL_POLYGON_BIT:
764 {
765 const struct gl_polygon_attrib *polygon;
766 polygon = (const struct gl_polygon_attrib *) attr->data;
767 _mesa_CullFace(polygon->CullFaceMode);
768 _mesa_FrontFace(polygon->FrontFace);
769 _mesa_PolygonMode(GL_FRONT, polygon->FrontMode);
770 _mesa_PolygonMode(GL_BACK, polygon->BackMode);
771 _mesa_PolygonOffset(polygon->OffsetFactor,
772 polygon->OffsetUnits);
773 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, polygon->SmoothFlag);
774 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, polygon->StippleFlag);
775 _mesa_set_enable(ctx, GL_CULL_FACE, polygon->CullFlag);
776 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_POINT,
777 polygon->OffsetPoint);
778 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_LINE,
779 polygon->OffsetLine);
780 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL,
781 polygon->OffsetFill);
782 }
783 break;
784 case GL_POLYGON_STIPPLE_BIT:
785 MEMCPY( ctx->PolygonStipple, attr->data, 32*sizeof(GLuint) );
786 ctx->NewState |= _NEW_POLYGONSTIPPLE;
787 if (ctx->Driver.PolygonStipple)
788 ctx->Driver.PolygonStipple( ctx, (const GLubyte *) attr->data );
789 break;
790 case GL_SCISSOR_BIT:
791 {
792 const struct gl_scissor_attrib *scissor;
793 scissor = (const struct gl_scissor_attrib *) attr->data;
794 _mesa_Scissor(scissor->X, scissor->Y,
795 scissor->Width, scissor->Height);
796 _mesa_set_enable(ctx, GL_SCISSOR_TEST, scissor->Enabled);
797 }
798 break;
799 case GL_STENCIL_BUFFER_BIT:
800 {
801 const struct gl_stencil_attrib *stencil;
802 stencil = (const struct gl_stencil_attrib *) attr->data;
803 _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
804 _mesa_ClearStencil(stencil->Clear);
805 _mesa_StencilFunc(stencil->Function, stencil->Ref,
806 stencil->ValueMask);
807 _mesa_StencilMask(stencil->WriteMask);
808 _mesa_StencilOp(stencil->FailFunc, stencil->ZFailFunc,
809 stencil->ZPassFunc);
810 }
811 break;
812 case GL_TRANSFORM_BIT:
813 {
814 GLuint i;
815 const struct gl_transform_attrib *xform;
816 xform = (const struct gl_transform_attrib *) attr->data;
817 _mesa_MatrixMode(xform->MatrixMode);
818 /* clip planes */
819 MEMCPY(ctx->Transform.EyeUserPlane, xform->EyeUserPlane,
820 sizeof(xform->EyeUserPlane));
821 MEMCPY(ctx->Transform._ClipUserPlane, xform->_ClipUserPlane,
822 sizeof(xform->EyeUserPlane));
823 /* clip plane enable flags */
824 for (i = 0; i < MAX_CLIP_PLANES; i++) {
825 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i,
826 xform->ClipEnabled[i]);
827 }
828 /* normalize/rescale */
829 _mesa_set_enable(ctx, GL_NORMALIZE, ctx->Transform.Normalize);
830 _mesa_set_enable(ctx, GL_RESCALE_NORMAL_EXT,
831 ctx->Transform.RescaleNormals);
832 }
833 break;
834 case GL_TEXTURE_BIT:
835 /* Take care of texture object reference counters */
836 {
837 GLuint u;
838 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
839 ctx->Texture.Unit[u].Current1D->RefCount--;
840 ctx->Texture.Unit[u].Current2D->RefCount--;
841 ctx->Texture.Unit[u].Current3D->RefCount--;
842 ctx->Texture.Unit[u].CurrentCubeMap->RefCount--;
843 }
844 MEMCPY( &ctx->Texture, attr->data,
845 sizeof(struct gl_texture_attrib) );
846 ctx->NewState |= _NEW_TEXTURE;
847 /* restore state of the currently bound texture objects */
848 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
849 copy_texobj_state( ctx->Texture.Unit[u].Current1D,
850 &(ctx->Texture.Unit[u].Saved1D) );
851 copy_texobj_state( ctx->Texture.Unit[u].Current2D,
852 &(ctx->Texture.Unit[u].Saved2D) );
853 copy_texobj_state( ctx->Texture.Unit[u].Current3D,
854 &(ctx->Texture.Unit[u].Saved3D) );
855 copy_texobj_state( ctx->Texture.Unit[u].CurrentCubeMap,
856 &(ctx->Texture.Unit[u].SavedCubeMap) );
857
858 ctx->Texture.Unit[u].Current1D->Complete = GL_FALSE;
859 ctx->Texture.Unit[u].Current2D->Complete = GL_FALSE;
860 ctx->Texture.Unit[u].Current3D->Complete = GL_FALSE;
861 ctx->Texture.Unit[u].CurrentCubeMap->Complete = GL_FALSE;
862 }
863 }
864 break;
865 case GL_VIEWPORT_BIT:
866 {
867 const struct gl_viewport_attrib *vp;
868 vp = (const struct gl_viewport_attrib *)attr->data;
869 _mesa_Viewport(vp->X, vp->Y, vp->Width, vp->Height);
870 _mesa_DepthRange(vp->Near, vp->Far);
871 }
872 break;
873 default:
874 gl_problem( ctx, "Bad attrib flag in PopAttrib");
875 break;
876 }
877
878 next = attr->next;
879 FREE( attr->data );
880 FREE( attr );
881 attr = next;
882 }
883 }
884
885
886 #define GL_CLIENT_PACK_BIT (1<<20)
887 #define GL_CLIENT_UNPACK_BIT (1<<21)
888
889
890 void
891 _mesa_PushClientAttrib(GLbitfield mask)
892 {
893 struct gl_attrib_node *newnode;
894 struct gl_attrib_node *head;
895
896 GET_CURRENT_CONTEXT(ctx);
897 ASSERT_OUTSIDE_BEGIN_END(ctx);
898
899 if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
900 gl_error( ctx, GL_STACK_OVERFLOW, "glPushClientAttrib" );
901 return;
902 }
903
904 /* Build linked list of attribute nodes which save all attribute */
905 /* groups specified by the mask. */
906 head = NULL;
907
908 if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
909 struct gl_pixelstore_attrib *attr;
910 /* packing attribs */
911 attr = MALLOC_STRUCT( gl_pixelstore_attrib );
912 MEMCPY( attr, &ctx->Pack, sizeof(struct gl_pixelstore_attrib) );
913 newnode = new_attrib_node( GL_CLIENT_PACK_BIT );
914 newnode->data = attr;
915 newnode->next = head;
916 head = newnode;
917 /* unpacking attribs */
918 attr = MALLOC_STRUCT( gl_pixelstore_attrib );
919 MEMCPY( attr, &ctx->Unpack, sizeof(struct gl_pixelstore_attrib) );
920 newnode = new_attrib_node( GL_CLIENT_UNPACK_BIT );
921 newnode->data = attr;
922 newnode->next = head;
923 head = newnode;
924 }
925 if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
926 struct gl_array_attrib *attr;
927 attr = MALLOC_STRUCT( gl_array_attrib );
928 MEMCPY( attr, &ctx->Array, sizeof(struct gl_array_attrib) );
929 newnode = new_attrib_node( GL_CLIENT_VERTEX_ARRAY_BIT );
930 newnode->data = attr;
931 newnode->next = head;
932 head = newnode;
933 }
934
935 ctx->ClientAttribStack[ctx->ClientAttribStackDepth] = head;
936 ctx->ClientAttribStackDepth++;
937 }
938
939
940
941
942 void
943 _mesa_PopClientAttrib(void)
944 {
945 struct gl_attrib_node *attr, *next;
946
947 GET_CURRENT_CONTEXT(ctx);
948 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
949
950 if (ctx->ClientAttribStackDepth == 0) {
951 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib" );
952 return;
953 }
954
955 ctx->ClientAttribStackDepth--;
956 attr = ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
957
958 while (attr) {
959 switch (attr->kind) {
960 case GL_CLIENT_PACK_BIT:
961 MEMCPY( &ctx->Pack, attr->data,
962 sizeof(struct gl_pixelstore_attrib) );
963 ctx->NewState = _NEW_PACKUNPACK;
964 break;
965 case GL_CLIENT_UNPACK_BIT:
966 MEMCPY( &ctx->Unpack, attr->data,
967 sizeof(struct gl_pixelstore_attrib) );
968 ctx->NewState = _NEW_PACKUNPACK;
969 break;
970 case GL_CLIENT_VERTEX_ARRAY_BIT:
971 MEMCPY( &ctx->Array, attr->data,
972 sizeof(struct gl_array_attrib) );
973 ctx->NewState = _NEW_ARRAY;
974 break;
975 default:
976 gl_problem( ctx, "Bad attrib flag in PopClientAttrib");
977 break;
978 }
979
980 next = attr->next;
981 FREE( attr->data );
982 FREE( attr );
983 attr = next;
984 }
985 }
986
987
988